Vi(vim) is a very common file editor for Linux, and many Linux distributions have vi(vim) installed by default. The vim commands are numerous but will greatly increase efficiency if used flexibly. Vi is an abbreviation for “visual interface” and vim is vi IMproved which is enhanced vi.
Here are a Vim commands cheat sheet for reference.
In general system management and maintenance, vi is enough. If you want to use code highlighting, you can use vim.
Basically, vi can be divided into three states, namely, a command mode, an insert mode, and the last line mode. The functions of each mode are as follows:
1) Command mode (command mode)
Controls the movement of the screen cursor, the deletion of characters, words or lines, moves, and copies a section, and enters the insert mode and bottom line mode.
2) Insert mode
Text input can only be done in insert mode, press ESC to return to command mode.
3) Last line mode
Save or exit vi, you can also set the editing environment, such as looking for strings, listing line numbers.
However, in general, we use vi to simplify vi into two modes, that is, the bottom line mode is also counted into the vim commands mode.
1. Open the file, save, close the file (used in vi command mode)
Vi filename //Open the filename file
:w //Save the file
:w w3cgeek.com //Save to w3cgeek.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
vim commands mode use, execute the following command will enter insert mode, press ESC key 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 (the beginning of a line that is not a null character)
O //Create a new line above the current line
o //Create a new line below the current line
R //Replace (overwrite) the current cursor position and some text following
J //Merge the line where the cursor is and the next line (still in command mode)
3. Move the cursor (used in vi command mode)
4. Delete, restore characters or lines (used in vi command mode)
5. Search (used in vi command mode)
/w3cgeek//Search the w3cgeek string under the cursor
?w3cgeek//Search the w3cgeek string on the cursor
n / / search down the previous search action
N //Search for the previous search action
6. Jump to the specified line (used in vi command mode)
n+ //jump down n lines
N- // jump up n lines
nG // jump to the line with line number n
G // jump to the bottom of the file
7. Set the line number (used in vi command mode)
:set nu //display line number
:set nonu // cancel display line number
8. Copy, paste (used in vi command mode)
Yy //Copy the current line to the buffer. You can also use “ayy copy,” a is the buffer, a can also be replaced with any letter from a to z, and multiple copy tasks can be completed.
Nyy / / copy the current line down n lines to the buffer, you can also use “anyy copy,” a is a buffer, a can also be replaced with any letter from a to z, you can complete multiple replication tasks.
Yw //Copy the characters from the beginning of the cursor to the end of the word.
Nyw //Copy n words starting from the cursor.
y^ //Copy the content from the cursor to the beginning of the line.
y$ //Copy the content from the cursor to the end of the line.
p //Paste the contents of the clipboard after the cursor. If you use the previous custom buffer, it is recommended to use “ap” for pasting.
P //Paste the contents of the clipboard in front of the cursor. If you use the previous custom buffer, it is recommended to use “aP” for pasting.
9. Replacement (used in vi command mode)
:s/old/new //Replace the first occurrence of the line in the new line with new
:s/old/new/g //Replace all old in the line with new
:n,m s/old/new/g //Replace all old from n to m lines with new
:%s/old/new/g //Replace all the old files in the current file with new
10. Edit other documents
:e otherfilename //Edit the file named otherfilename.
11. Modify the file format
:set fileformat=unix //Modify the file to Unix format. For example, the text file below win will appear in Linux under ^M.
Note: If you don’t know what mode you are in, you can press the Esc key twice to return to the command mode.
Read also: Vim commands cheat sheet