Linux Vi/Vim Commands Introduction

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.

Vim Commands

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)

Vi can directly use the cursor on the keyboard to move up and down, left and right, but the regular vi uses lowercase English letters h, j, k, l to control the cursor left, down, up, and right to move one grid.
Press Ctrl+b: the screen moves back one page.
Press Ctrl+f : the screen moves forward one page.
Press Ctrl+u: the screen moves back half a page.
Press Ctrl+d: the screen moves forward by half a page.
Press the number 0: move to the beginning of the current line.
Press G: to move to the end of the article.
Press $: to move to the end of the line where the cursor is located.
Press ^ : to move to the beginning of the line where the cursor is located.
Press w : the cursor jumps to the beginning of the next word.
Press e : The cursor jumps to the end of the next word.
Press b : the cursor returns to the beginning of the previous word.
Press #l: the # position of the cursor to move backwards, such as: 5l, 56l.

4. Delete, restore characters or lines (used in vi command mode)

x : Each time you press , delete the character after the cursor.
#x : Delete the following # characters at the cursor position. For example, 6x means to delete the last 6 characters of the cursor position.
X : Each time you press , delete the previous character at the cursor position.
#X : Delete the first # characters of the cursor position. For example, 20X means to delete the first 20 characters of the cursor position.
Dd : Delete the line where the cursor is.
#dd : Delete the # line from the line where the cursor is located.

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