SPOTO sorts out a comprehensive guide explaining basic vim commands that will be useful to any Linux user be it a sysadmin or a developer.
Command history
Commands starting with : and / have a history, you can type: or / and then press the up and down arrows to select a history command.
Start vim
Enter the following command in the command line window
Vim directly start vim
Vim filename Open vim and create a file named filename
File command
Open a single file
Vim file
Open multiple files at the same time
Vim file1 file2 file3 ...
Open a new file in the vim window
:open file
Open file in new window
:split file
Switch to the next file
:bn
Switch to the previous file
:bp
View the list of currently open files. The file currently being edited will be enclosed in [].
:args
Open remote files, such as ftp or share folder
:e ftp://192.168.10.76/abc.txt
:e \\qadrive\test\1.txt
Vim mode
Normal mode (press Esc or Ctrl+[Enter]. The file name is displayed in the lower left corner or is empty.
Insert mode (press i to enter) - INSERT-- in the lower left corner
Visual mode (do not know how to enter) Display in the lower left corner --VISUAL--
Navigation command
% brackets match
Insert command
i inserts in the current position
I insert at the beginning of the current line
a Insert after the current position
A is inserted at the end of the current line
o Insert a line after the current line
O insert a line before the current line
Find command
/text Find text, press n to find the next one, press N to find the previous one.
?text find text, reverse lookup, press n to find the next one, press N to find the previous one.
There are some special characters in vim that need to be escaped when searching. *[]^%/?~$
:set ignorecase ignores case lookups
:set noignorecase does not ignore case-sensitive lookups
Look for long words. If a word is long and you type it, you can move the cursor to the word. Press * or # to search for the word, which is equivalent to / search. And the # command is equivalent to? Search.
:set hlsearch Highlights the search results and all results are highlighted instead of just one match.
:set nohlsearch Turn off highlight search display
:nohlsearch Turns off the current highlight, and if you search again or press the n or N key, it will be highlighted again.
:set incsearch Step-by-step search mode to search for the currently typed characters without waiting for typing to complete.
:set wrapscan Re-search, when searching for the beginning or end of the file, return to continue searching, the default is enabled.
Replacement command
Ra Replace the current character with a, and the current character is the character of the cursor.
s/old/new/ replace new with old, replacing the first match of the current line
s/old/new/g Replace old with old, replacing all matches for the current line
%s/old/new/ Replace new with old, replacing the first match of all rows
%s/old/new/g Replace new with old, replacing all matches for the entire file
:10,20 s/^/ /g In line 10, the 20th line is preceded by four spaces in front of each line for indentation.
Ddp swaps the line of the cursor and the line immediately below it.
Move command
h shift one character to the left
l Move one character to the right. This command is rarely used. It is usually replaced by w.
k moves up one character
j move one character down
The above four commands can be used with numbers. For example, 20j is moving 20 lines downwards, and 5h is moving 5 characters to the left. In Vim, many commands can be used with numbers, such as deleting 10 characters 10x, after the current position. Insert 3! , 3a! <Esc>, Esc here is required, otherwise the command does not take effect.
w Moves a word forward (the cursor stops at the beginning of the word), and if it has reached the end of the line, it goes to the beginning of the next line. This command is fast and can be used instead of the l command.
b Move one word backward 2b Move back 2 words
e, with w, just the cursor stops at the end of the word
Ge, with b, the cursor stops at the end of the word.
^ Move to the first non-blank character on the line.
0 (number 0) moves to the first character of the line,
<HOME> Moves to the first character of the line. Same as 0 health.
$ Move to the end of the line 3$ Move to the end of the next 3 lines
Gg moves to the file header. = [[
G(shift + g) moves to the end of the file. = ]]
The f(find) command can also be used to move, fx will find the first character after the cursor, and 3fd will find the third character that is d.
F with f, reverse lookup.
Jump to the specified line, colon + line number, enter, such as jumping to 240 lines is: 240 enter. Another method is the line number +G, such as 230G jumps to 230 lines.
Ctrl + e scroll down one line
Ctrl + y to scroll up one line
Ctrl + d scroll down half screen
Ctrl + u scrolls up half a screen
Ctrl + f scroll down one screen
Ctrl + b scrolls up one screen
Undo and redo
u Undo (Undo)
U undo the operation of the entire line
Ctrl + r Redo, which is the undo of undo.
Delete command
x delete current character
3x deletes the current cursor starting three characters backward
X Deletes the previous character of the current character. X=dh
Dl delete the current character, dl=x
Dh delete the previous character
Dd delete the current line
Dj delete the previous line
Dk delete the next line
10d Deletes the first 10 lines of the current line.
D Delete the current character to the end of the line. D=d$
d$ deletes all characters after the current character (this line)
Kdgg deletes all lines before the current line (not including the current line)
jdG(jd shift + g) deletes all lines after the current line (not including the current line)
:1,10d delete 1-10 lines
:11, $d deletes 11 lines and all subsequent lines
:1, $d delete all rows
J(shift + j) deletes a blank line between two lines, actually merging two lines.
Copy and paste
Yy copy the current line
Nyy copies the current n lines, such as 2yy copy the current line and its next line.
p Paste after the current cursor. If you used the yy command to copy a line, paste it on the next line of the current line.
Shift+p paste before the current line
:1,10 co 20 Insert 1-10 lines after the 20th line.
:1,$co$ Make a copy of the entire file and add it to the end of the file.
Press v (word by word) or V (progressive line) to enter visual mode in normal mode, then use jklh command to move to select some lines or characters, then press y to copy
Ddp exchanges the current line and its next line
Xp exchanges the current character and the next character
Cut command
In normal mode, press v (word-by-word) or V (progressive) to enter the visual mode, then use the jklh command to move to select some lines or characters, then press d to cut
Nd cuts the n lines after the current line. Use the p command to paste the cut content
:1,10d Cut 1-10 lines. Use the p command to paste the cut content.
:1, 10 m 20 Move lines 1-10 after line 20.
Exit command
:wq save and exit
ZZ save and exit
:q! Forces an exit and ignores all changes
:e! Discard all changes and open the original file.
Window command
:split or new opens a new window with the cursor on the top window
:split file or :new file Open the file with a new window
The windows opened by split are horizontal, and you can use vsplit to open the window vertically.
Ctrl+ww moves to the next window
Ctrl+wj moves to the window below
Ctrl+wk to move to the upper window
close the window
:close The last window cannot use this command to prevent accidental exit from vim.
:q If it is the last closed window, it will exit vim.
ZZ Save and exit.
Close all windows, leaving only the current window
:only
Recording macro
Press q to add any letter to start recording, then press q to end recording (this means that macros in vim can't be nested), when using @加宏名, such as qa. . . q Record a macro named a, @a uses this macro.
Execute shell command
:!command
:!ls lists files in the current directory
:!perl -c script.pl Check the perl script syntax, you can not exit vim, very convenient.
:!perl script.pl Execute the perl script, you can not exit vim, very convenient.
:suspend or Ctrl - Z Hang vim, go back to the shell, press fg to return vim.
Comment command
The behavior comment in #perl program starts, so to comment some lines, just add ## at the beginning of the line.
3,5 s/^/#/g Notes Lines 3-5
3,5 s/^#//g Uncomment 3-5 lines
1, $ s / ^ / # / g Comment the entire document.
:%s/^/#/g Annotate the entire document, this method is faster.
Help command
:help or F1 shows the entire help
:help xxx Display xxx help, such as :help i, :help CTRL-[(ie Ctrl+[help).
:help 'number' Help for Vim options is enclosed in single quotes
:help <Esc> Help for special keys with <>
:help -t help with Vim startup parameters -
:help i_<Esc> Help for Esc in insert mode, help for pattern in mode _ theme mode
The content between || in the help file is a hyperlink, you can use Ctrl+] to enter the link, Ctrl+o(Ctrl + t) to return
Other non-editing commands
Repeat the previous command
:set ruler? Check if the ruler is set. In .vimrc, the options set using the set command can be viewed through this command.
:scriptnames View the location of the vim script file, such as .vimrc files, grammar files, and plugins.
:set list Displays non-printing characters such as tabs, spaces, end of line, and so on. If the tab cannot be displayed, be sure to set the .vimrc file with the set lcs=tab:>- command and make sure that there is a tab in your file. If expendtab is enabled, the tab will be expanded to a space.
Vim tutorial
On Unix systems
$ vimtutor
On a Windows system
:help tutor
:syntax lists the defined syntax items
:syntax clear Clear defined grammar rules
:syntax case match is case sensitive, int and int will be treated as different syntax elements
:syntax case ignore is case-insensitive, int and int will be treated as the same syntax element and use the same color scheme