The following commands are some of the shortcuts I commonly use. I use vscode as my editor and have the vim plugin installed.
Cursor movement
hjkl
: left down up rightgj
andgk
: move to the next or previous physical line, when a line appears linewrap, usingj
andk
does not move to the line as seen visually{
: jumps to the previous line at the beginning of the first paragraph found up}
: jumps to the next line at the end of the first paragraph sought down(
: jumps to the line at the beginning of the first paragraph of the upward search)
: jumps to the line at the end of the first paragraph of the downward searchw
: the beginning of the next wordb
: the beginning of the previous worde
: the end of the next wordge
: end of the previous wordgg
: jump to the first lineG
: skip to the last line<number> + gg
: jump to the number line:<number>
: jumps to the number line%
: match the corresponding parentheses and jump*
and#
: match the word the current cursor is on, move the cursor to the next or previous one
f
, F
, t
and T
, in-line cursor jumping
f
: the current line jumps to the specified character after the cursor, press;
to jump to the next specified characterF
: the current line jumps to the specified character before the cursor, press;
to jump to the next specified charactert
: the current line jumps to a character before the specified character after the cursor, press;
to jump to a character before the next specified characterT
: the current line jumps to one character after the specified character before the cursor, press ``;` to jump to one character after the next specified character
Generally, I only use f
and F
.
Mode switching
v
: normal mode enters visual mode V
: normal mode enters visual mode with the current line selected i
: normal mode enters insert mode with the cursor appearing before the currently selected character I
: normal mode enters insert mode with the cursor appearing at the beginning of the line a
: normal mode enters insert mode, the cursor appears after the currently selected character A
: normal mode enters insert mode, the cursor appears at the end of the line esc
: returns from visual and insert mode to normal mode o
: inserts a line below the current line and enters insert mode O
: inserts a line above the current line and enters insert mode.
Insert, Delete, Copy, Paste
The shortcuts related to insertion are already included in the mode switch.
gi
: enter insertion mode at the last place where the change was madex
: delete the character where the current cursor isdd
: delete the current linecc
: deletes the current line and enters insert modeD
: delete from the current cursor to the end of the lineC
: delete from the current cursor to the end of the line and enter insert modeyy
: copy the current lineY
: same asyy
yl
: copy the character under the current cursoryas
: copy a sentenceyap
: copy a paragraphp
: paste the clipboard after the cursorP
: paste the clipboard to the front of the cursorgP
: same asP
, except that the cursor is placed after the pasted contentgp
: same asp
, except that the cursor is placed after the pasted contentcw
: delete from the cursor location to the end of a word and enter insert mode (similarly you can delete withdw
but not enter insert mode)c$
: delete from the location of the cursor to the end of the line and enter insert mode (similarly, you can used$
to delete, but not enter insert mode)r
: replace the character under the current cursor, pressr
, then type a character to replace the current characterv + <select area> + <action>
: you can use v to enter visual mode, then select the area, and finally usey
,c
,d
andr
to operate on the selected area.
<command><pos>
mode: <command>
can be y
, d
, c
, etc., (a series of characters that can be used for cursor jumps) can be w
(end of a word), $
(end of a line), ^
(first non-empty character at the beginning of a line), b
(beginning of a word), 0
(beginning of a line), etc.
<verbs><adjectives><objects>
pattern: you can use operators (y
, d
, c
) as verbs, counts, a and i as adjectives, and moves (hjkl, w, $, ^, b, 0, etc.) as objects, e.g.:
d3l
: delete three characters to the right of the cursordl
: adjectives can be omitted, sodl
is equivalent tod1l
diw
: delete the word under the cursor excluding the surrounding spaces
Code folding
zc
: collapse codezo
: Expand codezM
: collapse allzR
: Expand all
Window movement
(1) Move the position of the current line
zz
: moves the line where the current cursor is to the middle of the windowzt
: moves the line where the current cursor is to the top of the windowzb
: move the current cursor line to the bottom of the window
(2) Line movement
ctrl + e
: move line by line downctrl + d
: move the window down by half the number of lines displayedctrl + y
: move up one line at a timectrl + u
: move the window up to show half of the number of rows
Split screen operation
(1) Create a split screen
Use the command.
:split [file]
or :sp [file]
: add a horizontal screen :vsplit [file]
or :vsp [file]
: add a vertical screen
Use the shortcut keys.
ctrl + w s
: add a horizontal screen with the current file ctrl + w v
: add a vertical screen with the current file
(2) Split screen switching
ctrl + w <arrow>
or ctrl + w <hjkl>
: Move the cursor to the split screen in the specified direction
(3) Close the screen and split screen
:only
: keep only the current split screen, close other split screens :ctrl + w c
: close the current window :ctrl + w q
: close the current window, if there is only one split screen, exit vim
(4) increase the screen height
:ctrl + w +
: increase the height :ctrl + w -
: decrease the height
Multiple cursor operations (block operations)
- Use
ctrl + v
to enter block operations - you can use
hkjl
,$
, etc. to insert multiple cursors and move cursors - use
I
andA
to enter edit
You can also use v
to enter visual mode, select some lines, and then press A
to insert a cursor at the end of each line to edit it.
Formatting operations in visual mode
v
enters visual mode, and after entering visual mode, you can select some rows with hjkl
. You can do the following operations on the selected rows.
J
: concatenate all rows into one row<
or>
: indent left and right=
: auto-indent, format the code (very nice)
Find and Replace
The replacement format is as follows, with support for regular expressions.
|
|
This command means search for pattern
in each line of range
and replace it with string
. count
is a positive integer multiplied by the command.
(1) Find
/<pattern>
: highlight what is found/\C<pattern>
: case-sensitive/\c<pattern>
: ignore case/\<<pattern>\>
: whole word match, note the modifier\<
in front here, followed by the modifier\>
, the combination of the two means whole word match*
: search down for the word under the current cursor#
: look up for the word under the current cursorn
: jump to the next found content:nohl
: unhighlight
(2) Current line replacement
:s/foo/bar/
: replace the first foo
found in the current line and replace it with bar
:s/foo/bar/g
: replace all foo
found in the current line and replace it with bar
(3) Full file replacement
:%s/foo/bar/
: replace first foo
in all lines of the current file and replace them with bar
:%s/foo/bar/g
: replace foo
in all lines of the current file and replace them with bar
(4) c
flag
:%s/foo/bar/gc
uses the c
flag to confirm each replacement in turn. The replace with foo(y/n/a/q/l)?
Confirmation dialog, press y
to replace the match, or press l
to replace the match and exit. Press n
to skip the current match, press q
or Esc
to quit replacing. The a
option replaces the match and all remaining matches.
(5) i
flag turns on case sensitivity
:s/foo/bar/gc
: foo
will not match Foo
(6) Specify the query range
:3,10s/foo/bar/g
: the query range is from the third line to the tenth line, replace foo
in the range with bar
:. ,$s/foo/bar/
: the query range is from the current line to the last line, replacing foo
in the range with bar
, .
means the current line, $
means the last line :. ,+4s/foo/bar/g
: the query range is four rows down from the current row (four rows in total), replace foo
in the range with bar
, .
means the current row, +4
means the next four rows.
<start position><command><end position>
For example 0y$
, means copy the current line.
0
: go to the beginning of the line firsty
: copy from<start position>
, here it is the beginning of the line$
: copies all the way to the end of the line
<command><a | i | s><obj>
s
requires plugin support, vscode-vim comes with this feature
a
means around, i
means inner, s
means surround, command
can be d
c
y
, obj
can be quotes, double quotes, w
and t
, t
means tag, which is more useful for markup languages like html
diw
: delete the word, not including the symbols around the word, for example" word"
, the cursor is inw
, after deleting it becomes""
daw
: delete the current word, including the surrounding blank characters, e.g." word"
, with the cursor atw
, becomes""
after deletionda"
: removes the contents of the middle of the double quote, including the double quote itself, e.g." word"
, with the cursor atw
, becomes `` after deletiondi"
: removes the contents of the middle of a double quote, excluding the double quote itself, e.g." word"
, with the cursor atw
, becomes""
after deletionds"
: removes the double quotes around the current word, e.g." word "
, with the cursor atw
,which becomes ``w` after deletion
If you replace the command d
above with c
, then the deletion will go into insertion mode.
s
is useful for writing markup languages like html, for example to change the following div
to p
, you can use cstt
and then type p
and c
for change
.
Peek definition and go to definiton
gh
: similar to mouse hover, will pop up the definition windowgd
: jump to definitions and references
Other
:edit [file-path]
: edit an existing file or create a new one