30 minutes to get started, VIM

Time:2021-8-15

For more interesting content, please pay attention to WeChat official account:Back end technology cabin

30 minutes to get started, VIM

VIM is a text editor developed from VI. it is one of the necessary tools for Linux back-end developers. It is jokingly called the God of the editor by the majority of code farmers (the God editor is Emacs, which I haven’t used before. It seems that the segment is higher than VIM). This paper briefly introduces the different modes in vim and the high-frequency commands in each mode, hoping to lead beginners to get started quickly.

VIM’s model

There are many modes in VIM, and this unique design is easy to confuse many beginners. You can switch freely between VIM’s multiple modes only through the keyboard.

Normal mode

After VIM is started, the default mode is normal mode, which is just the opposite of most editors (the default is insert mode). In this mode, the user can perform operations such as moving the cursor and deleting text. For example, jump to row N and delete row n data.

Insert mode

In this mode, most keys insert text to the current cursor.

Select mode

The selection mode is similar to selecting a paragraph of text with the mouse under windows. VIM is unique in that it only needs to press the key to select a piece of text.

Command mode

In command mode, VIM commands can be entered and executed by vim. Generally, search, replace, save file, exit file and other commands are executed in this mode

Switching between modes

  • In insertion mode, enter “ESC” to enter normal mode
  • In normal mode, enter “I” (or “a”, “O” in case, but with different meanings) to enter the insertion mode
  • In normal mode, enter “:” (or “/ /”, “?”, “!”), Enter command mode
  • In normal mode, enter “V” (or “V”) to enter the selection mode

VIM common commands

File navigation

In normal mode, the following commands can be executed to make the cursor quickly reach the target:

GG jump to the first line of the current file
G jump to the last line of the current file
%Jump to n% of the current file

^Jumps to the beginning of the current line of the cursor
$jumps to the end of the current line of the cursor
F jumps to the previous position after the first cursor in the current line

*Highlight the word at the cursor and jump to the next same word
#Highlight the word at the cursor and jump to the previous same word

%Jump to the bracket matching the brackets ("[", "{", "(") at the current cursor
{jump to the beginning of the current paragraph
}Jump to the end of the current paragraph
(jump to the beginning of the current sentence)
)Jump to the end of the current sentence
[[jump to the previous function / class (it is required that '{' in C + + code must occupy a separate line)
]]Jump to the next function (the requirements are the same as above)

CTRL + O jumps to the previous position in the history cursor position list
CTRL + I jumps to the next position in the history cursor position list

MX set bookmark, X can only be 26 letters of A-Z
`X jump to bookmark

Zr expand code collapse

File editing

In normal mode, you can edit the file by executing the following command

X deletes the character at the cursor position
DW delete word at cursor position
DT deletes all text from the cursor to the middle of the first character (excluding)
DD delete current row
DD delete n rows
DGG deletes all text from the first line to the current line of the file
DG deletes all text from the current line to the last line of the file

>>Indent current line of code
< < the current line of code is indented less
n> > indent n lines
N < < indent n lines less
=Format current line of code

R replace the character at the cursor position, and the new character is

YY copies the current row to the buffer
YY copy n rows to buffer
P copies the buffer to the next line
P copies the buffer to the previous line
J merges the next line of text into the current line

O add a blank line under the cursor, the cursor jumps to the beginning of the blank line and enters the insertion mode
O add a blank line to a line on the cursor, and the cursor jumps to the beginning of the blank line and enters the insertion mode

Text selection

In the selection mode, the following commands can be executed for text selection

V press V to enter the row selection mode, and then press the up or down key to select multiple rows
V press V to enter the column selection mode, then press up or down to select the row range, and press left or right to select the column range.

When the target text is selected, we can copy (y combined with P), delete (d) and overwrite the selected text.

Text search and replace

In command mode, the following commands can be executed to search and replace text

/Drill down in files
?                               Search up in files
N if ` / ` search is used, jump to the next position in the matching list; Otherwise, jump to the previous position in the matching list
N if ` / ` search is used, jump to the previous position in the matching list; Otherwise, jump to the next position in the matching list

/C file, ignoring case

: S / keyword1 / keyword2 / g replace all keyword1 in the file with keyword2
: 1100s / keyword1 / keyword2 / g replace all keyword1 to 100 lines in the file with keyword2
: 1100s / keyword1 / keyword2 / GC replaces keyword1 in lines 1 to 100 in the file with keyword2. A confirmation dialog box is displayed for each replacement

:!                         After exiting the VIM window, execute the bash command, and then return to the VIM window

:%! XXD view file contents in hexadecimal 
:%! XXD - R restitution

: set fileencoding to view the current file encoding

: goto jumps to the location in the file at offset

Some common combination operations

formatting code

Ggvg = all codes in the format file

Insert a column into the text

V first press V to enter the column selection mode, and then select the position of the inserted column through left / right, up / down
I press I to insert mode, and then enter the text ABC. You can see that a column ABC is inserted in all rows

Multiline replication

V first press V to enter the row selection mode, and then select multiple rows through the up / down key
Y copies the selected multiple rows to the buffer
        The cursor jumps to the position to paste
P copies multiple lines in the buffer to the cursor position

Convert a column of text to uppercase / lowercase

V press V to enter the column selection mode, and then select the position of the inserted column through left / right, up / down
U press u to convert the selected column of text to uppercase; Press u to convert it to lowercase

Recommended reading

More interesting content, please scan code concern WeChat official account:Back end technology cabin。 If you think the article is helpful to you, please share, forward and read it.

二维码