The vim command is a rich text editor and an extended version of the vi editor. It can be used to edit various texts, especially for editing programs. Compared with vi, it has many advantages and can be used many times. Undo, you can open multiple windows for editing, syntax highlighting, command line editing, online help, visual selection, etc. at the same time.
vim introduction
vim is a rich text editor, an extended version of vi editor, It can be used to edit various texts, especially programs.
Compared with vi, it has many advantages, can be undone multiple times, can open multiple windows for editing at the same time, syntax highlighting, command Line editing, online help, visual selection, etc.
vim basic operations
Open files
vim filename #example : Open a.txt file with vim editor vim a.txt
If the filename is empty, an empty file will be opened
vim [options] [filelist] The left and right square brackets represent a collection, which means that there can be multiple option and multiple lists
Five modes of vim
-
Normal Mode: This is the most basic mode. In any of the following modes, you can return to normal mode by clicking Esc
-
Insert mode: In this mode, you can enter a string through the keyboard , in the normal mode, press i, a, o to enter the insert mode, i means to insert from the character before the current cursor, a means to insert from the character after the current cursor, o means to insert from the character of the current cursor Insert the next line
-
Visual mode: Visual mode can select text, and then facilitate copying, pasting and other operations. In normal mode, only one character can be selected at the same time. So if you want to delete multiple characters or multi-line characters, it is inconvenient — of course, there are commands to solve this problem in normal mode, but it is a bit more complicated than visual mode. Press v in normal mode to enter the Visual mode, press V to enter visual line mode, and press Crtl+v to enter visual block mode. You can easily find the difference between these three visual modes by following your own practice. In addition, in the visual mode, you can also use v, V, Crtl+v to switch between the three visual modes
-
Replacement mode: press R in normal mode to enter Replacement mode, starting from the current cursor position, the characters entered by the keyboard will replace the existing characters in the text
-
Last line mode: press : and / to enter the last line mode, use The last line mode entered by / is mainly used to find strings, which will be explained in detail below
Detailed explanation of necessary commands in normal mode
h #move one character to the left l #move one character to the right j # move down one line k #move up one line dd #delete a whole line x #delete the character under the cursor u # Undo after deleting characters yy #copy current line #All of the above characters can be preceded by numbers, which means shifting n characters to the left, shifting n characters, copying n lines, etc... p # perform paste operation 0 # move to the beginning of the line g0 #move to the beginning of the current screen H #Move the cursor to the top line of the screen M #Move the cursor to the middle line of the screen L #Move the cursor to the bottom line of the screen gg # move to the head of the file G # move to the end of the file crtl + f # turn down a screen crtl + b #turn up a screen crtl + d # turn down half a screen crtl + u # turn half screen up n% #to the position of file n% zz #Move the current line to the center of the screen zt #Move the current line to the top of the screen zb #Move the current line to the bottom of the screen
Detailed explanation of common commands in the last line mode
:q #Exit the current file without saving, q is the abbreviation of quit :q! #Forcibly quit the current file :w #Save the current file, w is the abbreviation of write :w! # For read-only files, changes can be forced to be saved :wq #Save and exit :r filename #Insert a file at the current position :r !date #Insert the current date at the current position :r !command #Insert the output of other shell commands at the current position :% s/target character/replacement character/g #Replace the target character with the replacement character The last g stands for global replacement, % stands for all lines % s/target character/replacement character/gic #replace the target character with the replacement character, % means all lines, the last g means global replacement, i means ignore case, c means each replacement requires user confirmation :1,4 s/target character/replacement character/g #Replace the target character from the first line to the fourth line with the replacement character /string : Find the string in the following text
Detailed explanation of common commands in visual mode
d #delete selected characters y #copy the selected character p #Paste selected characters
Related learning recommendation: linux video tutorial
The above is the detailed content of vim commands, please pay attention to 1024programmer for more. com Other related articles!