vim command summary

Please indicate the source for reprinting: https://blog.csdn.net/jinixin/article/details/80211377


The vim editor is often used in work, so here is a brief summary of vim. Put aside the complexity, pick out the commonly used commands, make it easy to remember and improve work efficiency.



Overview


Speaking of vim, I have to mention vi. vi is a lightweight non-graphical text editor. The "UNIX Program Compatibility Standard" requires that the system must be equipped with vi, and most Linux is equipped with an enhanced version of vim. Compared with vi, vim has a better user experience. You can know the editor version used by the system by typing "vi" on the command line.

The following uses the vim command to open the xx file:

1) vim xx: open the xx file and position the cursor to the first line

2) vim + xx: open the xx file and position the cursor to the last line

3) vim +3 xx: open the xx file and position the cursor to line 3



three modes


vim has three common modes: command mode, bottom line mode and insert mode


command mode

Use vim to open a file, and enter the command mode. At this time, vim waits for the user to enter a command, and the content of the file cannot be modified.


input mode

Modify file content


bottom row mode

Type ":" in command mode to enter bottom line mode


Switch between three modes

Command Mode -> Input Mode: i, o, etc. to insert commands

Input Mode -> Command Mode: Press the "Esc" key

Command Mode -> Bottom Line Mode: Press the ":" key


When you are not sure which mode you are in, press the "Esc" key twice to return to the initial state



Command Mode Common Commands


edit operation

i: switch from command mode to input mode

o: Insert a line below the line where the cursor is located and switch to input mode

O: Insert a line above the line where the cursor is located and switch to input mode


undo operation

u: undo the previous operation


mobile operation

w: the cursor moves back one word (note that it is not a character)

b: the cursor moves forward one word (note that it is not a character)

^: move the cursor to the beginning of the current line

$: move the cursor to the end of the current line

G: Move the cursor to the end of the file

Number G: the cursor moves to the "number" line, such as "6G" means the cursor moves to the 6th line

gg: move the cursor to the beginning of the file


delete operation

dd: delete the line where the cursor is located (actually cut the current line)

x: delete the character where the cursor is located


Copy operation

yy: Copy the line where the cursor is located


cut operation

Use the "v" command and the arrow keys to select the area first, and then use the following commands:

d: cut the selected content

y: copy the selected content


paste operation

p: paste below the line where the cursor is located

P: Paste above the line where the cursor is located


search operation

/xxx: Search xxx backward from the cursor, press "n" to switch to the next matching result

?xxx: Search xxx forward from the cursor, press "n" to switch to the previous matching result



Common commands in bottom line mode


exit operation

q: quit

!: Force execution (often used in conjunction with q to discard changes and exit)

w: save

The resulting combination, wq: save and exit, q!: discard changes and exit


positioning operation

Numbers: Go to row "number"


replace operation

%s/A/B/gc

illustrate:

%: Indicates that the command operation range is the entire article, such as "1,5s/A/B/gc", the specified command operation range is 1 to 5 lines

/A/B/: replace A with B

c: The user will be confirmed before each replacement. Although it is optional, it is recommended to write it. The following are common replies, y: perform replacement, n: skip here, a: perform all replacements here and after, q or "Esc" key: exit replacement


custom display

set nu: set the line number

syntax on: syntax highlighting

set mouse=a: the mouse is available

Although the custom display can be operated in bottom line mode, it will be invalid after exiting vim. If you want to be effective for a long time, you need to write these configurations into the ".vimrc" file in the user directory (ie "vim ~/.vimrc"), and it will take effect after saving.



If inappropriately pointed out in the text, I hope to accommodate and point out, thank you


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561494&siteId=291194637