Summary of common Vim commands

Source: Internet
Author: User
1. Delete characters
To delete a character, you only need to move the cursor over the character and press "X ".


2. delete a row
Use the "DD" command to delete a whole line of content. After deletion, the following lines will be moved up to fill the gap.


3. Delete linefeed
In vim, You can merge the two rows into one row, that is, the line break between the two rows is deleted: The command is "J ".


4. Undo
If you accidentally delete too much content, use the "U" command to cancel the last operation.


5. Redo if you undo it multiple times, you can also use the CTRL-R (redo) to reverse the Undo action. In other words, it is an undo action. The undo command also uses the "U" command to undo all operations on a row at a time. The second time you use this command, the previous "U" operation will be revoked. With "u" and CTRL-R you can retrieve any operational status.


6. append
The "I" command inserts text before the current cursor.
The "A" command inserts text after the current cursor.
The "O" command can start another line under the current line and convert the current mode to the insert mode.
The "O" command (note that the uppercase letter O is used) will start a new line above the current line.


7. Exit
To exit vim, run the "ZZ" command ". This command saves the current file and exits vim.


8. Discard editing
Discard all the modifications and exit. Run the ": Q!" command! ". Use ": E! "Command to discard all modifications and reload the original content of the file.


9. Move to the beginning or end of a row
The "$" command moves the cursor to the end of the current line. If you have a key on your keyboard, it works the same way. The "^" command moves the cursor to the first non-blank character in the current line. The "0" command always moves the cursor to the first character of the current line. The "$" command also accepts a count. For example, "1 $" moves the cursor to the end of the current row, and "2 $" moves to the end of the next row, and so on. The "0" command cannot accept a count like this. Adding a count before the "^" command does not work.


10. Move to the specified row
Use the "G" command to specify a command count, which then positions the cursor on the line specified by the command count. For example, "33 GB" will place the cursor on the 33rd rows. If the command count is not specified as a parameter, "G" positions the cursor on the last line. The "GG" command is a quick way to jump to the first line.


11. display the row number
Use "set number" to display a row number before each row. Rather, use the command ": Set nonumber" to close the row number"


12. Simple search
The "/string" command can be used to search for a string. Use the "N" command to find the next position of the string that was last searched.


13. Search for the next word in the text
Place the light on the word and press the "*" key. Vim retrieves the word of the current cursor and searches for the target string. "#" Command is the reverse version. You can also add a command count before the two Commands: "3 *" to find the third appearance of the word under the current cursor.


14. Search for the entire word
If you use "/The" to find Vim, it matches "there ". To search for "the" as an independent word, run the following command: "/the \> ". "\>" Is a special note that matches only the end of a word. Similarly, "\ <" matches the beginning of a word. In this way, you can use the word ":"/\".


15. Highlight search results
To enable this function, you can use ": Set hlsearch" to disable this function: ": Set nohlsearch ". To remove the current highlight, run the following command: ": nohlsearch" (can be abbreviated as Noh ).


16. Match the beginning and end of a row
^ Match the beginning of a line. $ Match the end of a line.
Therefore, "/was $" only matches the word was at the end of a row, so "/^ was" only matches the word was at the beginning of a row.


17. match any single character
. This character can match any character. For example, "C. M" can match any character whose first character is C and whose last character is m, regardless of the character in the middle.


18. Match special characters
Put a backslash before a special character. If you search for "ter. ", Use the command"/TER \. "


19. operator commands and displacement
The "DW" command can delete a word, and the "d4w" command is to delete four words, and so on. Similar to "d2e" and "d $ ". Such commands have a fixed mode: operator commands + displacement commands. First, enter an operator command. For example, "D" is a delete operator. Next is a shift life. For example, "W ". In this way, any move of the cursor command is the scope of the command.


20. Change text
The operator command is "C". Change the command. Its behavior is similar to that of the "D" command, but it enters the insert mode after the command is executed. For example, "CW" changes a word. Or, more accurately, it deletes a word and puts you in the insert mode.
The "cc" command can change the entire line. However, the original indentation remains unchanged.
"C $" changes the content from the current cursor to the end of the line.
Shortcut command: X represents DL (delete the characters under the current cursor)
X indicates DH (delete the character on the left of the current cursor)
D Represents d $ (content deleted to the end of the row)
C stands for C $ (content modified to the end of the line)
S stands for Cl (modify one character)
S stands for CC (modify a whole line)
Both the "3dw" and "d3w" commands delete three words. The first command "3dw" can be regarded as the operation to delete a word three times; the second command "d3w" is to delete three words at a time. This is not an obvious difference. In fact, you can put the command count in both places. For example, "3d2w" deletes two words and repeats the command three times, with a total of six words.


21. replace a single character
The "R" command is not an operator command. It waits for you to enter the next character to replace the character under the current cursor. The marker before the "r" command replaces multiple characters with the character to be entered. Replace a character with a linefeed using "R ". It deletes a character and inserts a line break. In this example, only the specified number of characters are deleted using the command note: "4R" will replace the four characters with a line break.


22. Copy text (copy in VIM editor)
The "Y" operator command copies the text to register 3. Then you can use the "P" command to retrieve it. Because "Y" is an operator command, you can use "yw" to copy a word. You can also use the command count. In the following example, the "y2w" command is used to copy two words, and the "YY" command is used to copy an entire line. "Y" is also used to copy the entire line, the command to copy the current cursor to the end of the line is "Y $ ".


23. Quick commands
X Delete the characters under the current cursor ("DL" shortcut command)
X Delete the character before the current cursor ("DH" shortcut command)
D. Delete the content from the current cursor to the end of the line ("d $" shortcut command)
DW deletes the cursor from the current cursor to the beginning of the next word
The dB is deleted from the current cursor to the beginning of the previous word.
DIW deletes the word with the current cursor (excluding blank characters)
DAW deletes the word with the current cursor (including blank characters)
DG deletes content from the current row to the end of the file
DGG deletes contents from the current row to the file header
If you use the "C" command instead of "D", all these commands are changed to change commands. Using "Y" is the Yank command, and so on.


24. Edit another file
Use the command ": Edit foo.txt", which can also be abbreviated as ": E foo.txt ".


25. File List
You can specify to edit multiple files when starting Vim. Run the command "Vim one. C Two. c Three. c ". Vim only displays the first file after it is started. After editing the file, you can run the following command: ": Next" or ": N "to save the work results and edit the next file, the command": wnext "or": WN "can be merged.


26. display the file being edited
Run the ": ARGs" command ".


27. Edit another file list
You can redefine a file list without restarting vim. The command ": ARGs five. c six. c Seven. H" defines the three files to be edited.


28. Automatic Storage
Command ": Set autowrite", "set Aw ". Automatically write the content back to the file: if the file has been modified, the content will be written to the file in each: Next,: rewind,: Last,: first,: Previous,: Stop,: suspend,: Tag ,:! ,: Make, Ctrl-], and Ctrl-^ commands.
Command ": Set autowriteall", "set Awa ". Similar to 'autowrite', but also applies to ": edit", ": enew", ": Quit", ": qall", ": Exit ",": xit ",": recover ", and close the vim window. Setting this option also means that Vim acts like opening 'autowrite.


29. view files
You can edit a file in read-only mode without writing content to the file. Run the following command:
Vim-r file.
If you want to forcibly avoid modifying the file, you can use the following command:
Vim-M file.


30. Change the file name
Save the existing file as a new file and run the command ": SAV (EAS) Move. c ". If you want to change the file name that is being edited but do not want to save the file, run the following command: ": F (ILE) Move. c ".


31. Close the window
Run the "close" command to close the current window. In fact, all commands ": Quit" and "ZZ" that exit the file editing will close the window, but ": Close" will prevent you from closing the last vim, to avoid accidentally disabling vim.
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.