| 1. Open, Save, Close Files |
vim filename // Open the filename file |
| :w // Save file |
| :w https://www.masters-tool.com // Save to https://www.masters-tool.com file |
| :q // Exit the editor, if the file has been modified, please use the following command |
| :q! // Exit the editor without saving |
| :wq // Exit the editor and save the file |
| 2. Insert Text or Line (Press ESC to exit insert mode) |
a // Add text to the right of the current cursor position |
| i // Add text to the left of the current cursor position |
| A // Add text at the end of the current line |
| I // Add text at the beginning of the current line (at the start of the non-empty line) |
| O // Create a new line above the current line |
| o // Create a new line below the current line |
| R // Replace (overwrite) the text at and after the current cursor position |
| J // Merge the current line and the next line into one line (still in command mode) |
| 3. Move Cursor |
Use the up, down, left, and right arrow keys |
| In command mode: h left, j down, k up, l right. |
| Spacebar right, Backspace left, Enter moves to the next line start, - moves to the previous line start. |
| 4. Delete, Restore Characters or Lines |
x // Delete the current character |
| nx // Delete n characters starting from the cursor |
| dd // Delete the current line |
| ndd // Delete n lines including the current line |
| u // Undo the last operation |
| U // Undo all operations on the current line |
| 5. Search |
/atool // Search for the string "atool" below the cursor |
| ?atool // Search for the string "atool" above the cursor |
| n // Search down for the previous search term |
| N // Search up for the previous search term |
| 6. Jump to Specified Line |
n+ // Jump down n lines |
| n- // Jump up n lines |
| nG // Jump to line number n |
| G // Jump to the end of the file |
| 7. Set Line Number |
:set nu // Show line numbers |
| :set nonu // Hide line numbers |
| 8. Copy, Paste |
yy // Copy the current line to the clipboard, you can also use "ayy to copy, "a is the buffer, a can be replaced with any letter from a to z, to complete multiple copy tasks. |
| nyy // Copy the current line and the next n lines to the clipboard, you can also use "anyy to copy, "a is the buffer, a can be replaced with any letter from a to z, to complete multiple copy tasks. |
| yw // Copy characters from the cursor to the end of the word. |
| nyw // Copy the next n words starting from the cursor. |
| y^ // Copy content from the cursor to the beginning of the line. |
| y$ // Copy content from the cursor to the end of the line. |
| p // Paste the content of the clipboard after the cursor, if a custom buffer is used before, it is recommended to use "ap for pasting. |
| P // Paste the content of the clipboard before the cursor, if a custom buffer is used before, it is recommended to use "aP for pasting. |
| 9. Replace |
:s/old/new // Replace the first occurrence of "old" with "new" in the line |
| :s/old/new/g // Replace all occurrences of "old" with "new" in the line |
| :n,m s/old/new/g // Replace all occurrences of "old" with "new" from line n to line m |
| :%s/old/new/g // Replace all occurrences of "old" with "new" in the current file |
| 10. Edit Other Files |
:e otherfilename // Edit the file named otherfilename. |
| 11. Modify File Format |
set fileformat=unix // Change the file to Unix format, for example, text files under Windows may have ^M in Linux. |