Common commands for text editor Vi

Source: Internet
Author: User

Vi is the most common text editor on Linux/Unix. It is used to create, edit, and display text files. Www.2cto.com Vi has no menu and only commands. Vi filename enters the command mode, and command mode enters the insert mode, you can use I, a, o key 1, insert command a after the cursor append text A at the end of the line attachment text I insert text before the cursor I insert text at the beginning of the line o insert text at the cursor insert text below O insert text on the cursor 2. Locate command h or move left a character j or move down a line k or move up a line l or right-click to move one line right character $ move to the end of the line 0 move to the beginning of the line H move to the top of the screen M move to the center of the screen L move to the bottom of the screen: set nu sets the row number: set nonu cancels the line number gg to the first line G to the last line nG to the n line: n to line n 3. delete command x Delete the character nx where the cursor is located Delete the next n characters dd Delete the line where the cursor is located ndd Delete n lines dG Delete the line at the end of the cursor d. Delete the content from the row where the cursor is located to the end: n1, n2d Delete row in the specified range 4. Copy and cut command yy or Y copy The current row nyy or nY copies the current row n rows dd cut the current row ndd cut the n rows below the current row p or P paste in the current cursor row or line 5, replace and cancel command r replaces the character R at the cursor position and starts to replace the character at the cursor position, press Esc to end u to cancel the previous step. 6. Search for and replace the command/string to search for the specified command forward. case-insensitive: set ic is ignored during search, for example, search for ftp: /ftp n searches for the next occurrence location of the specified string: % s/replaced string/replaced character to create/g, for example, replacing full-text ftp with hello: % s/ftp/hello/g: n1, n2s/replaced string/replaced with the character to create/g to replace the string within a certain range 7. Save the exit command: wq or ZZ save and quit: q! Do not save and exit: wq! Force save and exit (root or file owner) 8. Application Instance 1) Import file in vi: r file, for example, abc.txt file under import/testdirectory: r/test/abc.txt 2) run the following command in vi ::! Command example: view the file details in the/test directory in the vi Editor :! In this case, you do not need to exit vi and then execute the command 3) import the command execution result to vi. For example, import the result of executing the date command to vi: r! Date 4) define the shortcut key: commands triggered by the map shortcut key, such as: map ^ p I # <ESC> ^ indicates the shortcut key defined. Note that in vi, ^ this symbol is used to combine ctrl + v and ^ P in vi, ctrl + v and ctrl + p, or ctrl + v + p: map ^ p I # <ESC> indicates that no matter where the cursor is located in the row, you only need to press ctrl + p to insert the # sign at the beginning of the row, and then return to the command mode. I indicates to insert text at the beginning of the line, and <ESC> indicates to return to the command mode.: Map ^ E acodeartisan@gmail.com defines a shortcut ctrl + e, as long as you press ctrl + e you can enter my mailbox acodeartisan@gmail.com at the cursor position, very convenient. For example, if we want to define a shortcut key, no matter where the cursor is in the row, press ctrl + B, you can remove the # comment at the beginning of the line (in fact, delete the first character at the beginning of the line): map ^ B 0x defines a shortcut key ctrl + B. 0 indicates moving to the beginning of the line, and x indicates deleting the character where the cursor is located. 5) continuous line comment: n1, n2s/^/#/g Add at the beginning of the line between n1 and n2 # Note: n1, n2s/^ # // g Replace the # comment at the beginning of the line between n1 and n2 with a press ENTER or space, myemail will become acodeartisan@gmail.com: unab cancel the replacement operation of some of the above shortcuts, replace command operation after exiting vi will expire, because vi does not save the configuration. To save the configuration, you can write the commands in the configuration file/root/. vimrc of vi.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.