Vim has been in use for a few years now, but I still Google a lot of shortcuts every time I need to use them. I can only remember a few commands, and the search process is still a waste of time. Here are some commonly used Vim commands for future review.
The cursor
The command | Function (explanation) |
---|---|
h,j,k,l | H for left, J for down, K for up, l for right |
Ctrl+f | The previous page |
Ctrl+b | The next page |
w, e, W, E | Skip to the end of the word, lowercase including punctuation |
b, B | Move the cursor forward in words. Lowercase includes punctuation |
0 | Jump to the head of the line |
O | Open a new line |
^ | The beginning of a line |
$ | The end of a line |
gg | The first line of the document |
[N]G | The NTH line of the document (G is the last line), e.g. 27+ Shift + G |
insert
The command | Function (explanation) |
---|---|
i | Insert in front of the cursor |
I | Insert at the beginning of the line |
a | Insert after the cursor |
A | Insert to the end of the line |
o | Inserts a new line below the current cursor |
O(Shift+o) | Inserts a new line above the current cursor |
Esc | Turn off insert mode |
The editor
The command | Function (explanation) |
---|---|
r | Replaces a character with the cursor in insert mode |
J | Merge the next line to the previous line |
s | Deletes a character at the cursor position while the cursor is still on the line |
S | Deletes the line where the cursor is, while the cursor is still on the line, unlike DD |
u | Undo the previous step |
ctrl+r | Restore the previous operation |
. | Repeat the last command |
~ | Convert to uppercase |
[N]>> | Move one TAB to the right in one row or N rows |
[N]<< | Move one TAB to the left for one row or N rows |
exit
The command | Function (explanation) |
---|---|
:w | save |
:wq,:x | Save and close |
:q | Close (saved) |
:q! | Forcibly close, do not save |
Find and search
The command | Function (explanation) |
---|---|
/pattern | Search (non-insert mode) with re support |
? pattern | After the search |
n | The cursor reaches the previous target of the search result |
N | The cursor reaches the latter destination of the search result |
r+p | Replace the character after the cursor with the letter P |
:s/word/replace | Replace the first word in the cursor line with replace. |
:%s/word/replace/ | Find word and replace with replace |
: 1, 50 s/word/replace / | Search and replace between lines 1 and 50 inclusive |
:45s/word/replace/ | Indicates that search and replace is performed only on line 45. The 1,$line number range is equivalent to % |
%s/^/ The string to insert | Insert string at the beginning of each line |
%s/$/ The string to insert | Insert a string at the end of each line |
Cut, copy and paste
The command | Function (explanation) |
---|---|
dd | Deletes a line and saves the deleted content to the clipboard |
de | Delete the word content after the cursor and save the deleted content on the clipboard |
dw | Delete the word content after the cursor and the space after it, and save the deleted content on the clipboard |
dw | Delete a word |
[N]dd | Delete n lines starting with the current line |
x | Deletes the last character |
X | Deletes the previous character |
D | Deletes the last character of a line |
[N]yy | Copy one row or N rows |
yw | Copy a word |
p | paste |
The Windows operating
The command | Function (explanation) |
---|---|
:split | A window is split horizontally |
:vsplit | A window is split vertically |
:close | Close the window |
Ctrl+W | Switch Windows, h to the left window, J to the lower window, K to the upper window, L to the right window |