Skip to content

VIM Cheat Sheets

Cursor Movement

In NORMAL mode.

Keys Description
H    (or Left) Move cursor to the left (you may use arrow keys if .vimrc is set correctly)
J    (or Down) Move cursor down (you may use arrow keys if .vimrc is set correctly)
K    (or Up) Move cursor up (you may use arrow keys if .vimrc is set correctly)
L    (or Right) Move cursor to the right (you may use arrow keys if .vimrc is set correctly)
n H    (or n Left) Move cursor n steps to the left (you may use arrow keys if .vimrc is set correctly)
n J    (or n Down) Move cursor n steps down (you may use arrow keys if .vimrc is set correctly)
n K    (or n Up) Move cursor n steps up (you may use arrow keys if .vimrc is set correctly)
n L    (or n Right) Move cursor n steps to the right (you may use arrow keys if .vimrc is set correctly)
G G Go to the first line
Shift + G Go to the last line
} Jump to next paragraph
{ Jump to previous paragraph
: n Go to line number n

Edit

In NORMAL mode.

Commands Description
:e Edit file (i.e., open)
:w Write file (i.e., save)
:q Quit file (only works if file is unchanged)
:q! Force quit (even if file is changed)
:wq Write file and quit
Keys Description
Y Y Yank (i.e., copy) a line
D D Delete (i.e., copy) a line
P Paste after cursor
Shift + P Paste before cursor
U Undo
Ctrl + R Redo
= Auto-indent current line
n Y Y Yank (i.e., copy) n lines
n D D Delete (i.e., copy) n lines
n = = Auto-indent n lines
G G =     Shift + G Auto-indent all lines

The last keys can be split into 3 components:

  1. G G: Go to the first line
  2. =: Auto-indent
  3. Shift + G: Go to the last line

This reads as (1.) go to the first line, then (2.) auto-indent (3.) until the last line.

Search and Replace

In NORMAL mode.

Commands Description
/<pattern> Search for <pattern>
?<pattern> Search backward for <pattern>
:%s/<old>/<new>/gc Replace all <old> with <new> (this will prompt options)
:<sn>,<en>s/<old>/<new>/gc Replace all <old> with <new> in the given line range from <sn> to <en> (this will prompt options)

Once a search is being performed.

Keys Description
N Continue search forward
Shift + N Continue search backward

Options for replace

Option Description
y Yes, replace the current one
n No, skip this one
a Yes to all
l Yes to just this one
q Quit

Comment and Uncomment

We can quickly comment a specific range of lines using the replace functionality. The idea is to replace the beginning of a line with // (i.e., start of single line comment). We need to "escape" the / character by writing it as \/\/To match the beginning of the line, the character is ^.

Comment line 4 to 17.

1
:4,17s/^/\/\//gc

We can also quickly uncomment specific range of lines by replacing // with nothing.

Uncomment line 4 to 17.

1
:4,17s/\/\///gc

Simply replace 4 and 17 with the range that you need.

Split Screen

Commands

Keys Description
:sp <filename> Open file name <filename> in horizontal split screen
:vsp <filename> Open file name <filename> in vertical split screen
:e <filename> Open the file name <filename> in the current screen
Keys Description
Ctrl + W     Ctrl + W Change screen
Ctrl + W     H Change to the right screen
Ctrl + W     J Change to the down screen
Ctrl + W     K Change to the up screen
Ctrl + W     L Change to the left screen

Java

Keys Description
:!javac <filename>.java Compile the java file <filename>.java
:!java <classname> Run the class <classname>
:!jshell <filename> <filename> ... Start JShell with the given <filename>s