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 right
  • gj and gk: move to the next or previous physical line, when a line appears linewrap, using j and k 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 search
  • w: the beginning of the next word
  • b: the beginning of the previous word
  • e: the end of the next word
  • ge: end of the previous word
  • gg: jump to the first line
  • G: 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 character
  • F: the current line jumps to the specified character before the cursor, press ; to jump to the next specified character
  • t: the current line jumps to a character before the specified character after the cursor, press ; to jump to a character before the next specified character
  • T: 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 made
  • x: delete the character where the current cursor is
  • dd: delete the current line
  • cc: deletes the current line and enters insert mode
  • D: delete from the current cursor to the end of the line
  • C: delete from the current cursor to the end of the line and enter insert mode
  • yy: copy the current line
  • Y: same as yy
  • yl: copy the character under the current cursor
  • yas: copy a sentence
  • yap: copy a paragraph
  • p: paste the clipboard after the cursor
  • P: paste the clipboard to the front of the cursor
  • gP: same as P, except that the cursor is placed after the pasted content
  • gp: same as p, except that the cursor is placed after the pasted content
  • cw: delete from the cursor location to the end of a word and enter insert mode (similarly you can delete with dw 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 use d$ to delete, but not enter insert mode)
  • r: replace the character under the current cursor, press r, then type a character to replace the current character
  • v + <select area> + <action>: you can use v to enter visual mode, then select the area, and finally use y, c, d and r 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 cursor
  • dl: adjectives can be omitted, so dl is equivalent to d1l
  • diw: delete the word under the cursor excluding the surrounding spaces

Code folding

  • zc: collapse code
  • zo: Expand code
  • zM: collapse all
  • zR: 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 window
  • zt: moves the line where the current cursor is to the top of the window
  • zb: move the current cursor line to the bottom of the window

(2) Line movement

  • ctrl + e: move line by line down
  • ctrl + d: move the window down by half the number of lines displayed
  • ctrl + y: move up one line at a time
  • ctrl + 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)

  1. Use ctrl + v to enter block operations
  2. you can use hkjl, $, etc. to insert multiple cursors and move cursors
  3. use I and A 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.

1
:[range]s/<pattern>/[string]/[flags] [count]

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 cursor
  • n: 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 first
  • y: 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 in w, after deleting it becomes ""
  • daw: delete the current word, including the surrounding blank characters, e.g. " word", with the cursor at w, becomes "" after deletion
  • da": removes the contents of the middle of the double quote, including the double quote itself, e.g. " word", with the cursor at w, becomes `` after deletion
  • di": removes the contents of the middle of a double quote, excluding the double quote itself, e.g. " word", with the cursor at w, becomes "" after deletion
  • ds": removes the double quotes around the current word, e.g. " word ", with the cursor at w,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.

1
2
3
<div>
    This is a paragraph
</div>

Peek definition and go to definiton

  • gh: similar to mouse hover, will pop up the definition window
  • gd: jump to definitions and references

Other

  • :edit [file-path]: edit an existing file or create a new one