Vi and Vim Quick References: A One Page Cheat Sheet

Last Updated: 

Have you ever felt slow working with Vi/Vim?

Check this one-page cheat sheet that will help you get back to work quickly and improve your velocity while working in Vi/Vim.

Vi & Vim Cheat Sheet in PDF format

This is a core essentials Vi & Vim cheat sheet which cover the basic quick references of Vi insert mode, edit commands, ex commands, navigation, search and substitute, etc.

👉 Not sure between Vi and Vim? Check my post on the Differences Between Vi and Vim.

Insert & Replace

i insert text before the cursor
a insert text after the cursor
I insert text at the beginning of the current line
A insert text at the end of the current line
o insert text in a new line below the cursor
O insert text in a new line above the cursor
r replace a character at the cursor position
R replace characters starting at the cursor position

Cursor Motion

h or move cursor left
l or move cursor right
k or move cursor up
j or move cursor down
G goto the end of file
nG or :n goto the line number n
0 move to the beginning of the line
$ move to the end of the line
CTRL+f move one screen view forward
CTRL+b move one screen view backward

Ex Commands

:q quit current open file
:q! force to quit without saving changes
:w save file
:w file save file as file
:wq! force to save file then quit
:x,yw file write from line x to line y into file
:w >> file append buffer to file
:e file edit another file
:e! file edit another file without saving current changes
:r file insert file content at the current cursor position
:n edit next file in vi arguments file list
CTRL+G display current file name and position

Search & Substitute

/string search forward for string
?string search backward for string
n repeat the last search
:,$s/str1/str2/gc search and substitute str1 by str2 from the current line to the end of file. A line number can be specified before the comma ",". The "c" ask for confirmation before each substitution
:%s/str1/str2/g replace all str1 by str2 in all the file without prompting for confirmation
:%s/str1/str2/ replace str1 by str2 on the first occurrence in each line of the file

Undo, Delete, and Copy

u undo last change
CTRL+r redo last undo change
mA set mark A to current position. Marks can be the letter [a-z] and [A-Z]
y'A yank (copy) from current line to mark A
d'A delete from current line to mark A
P put the buffer content before the cursor
p put the buffer content after the cursor
x delete character at cursor position
dW delete first word after cursor position
d$ or D delete from cursor position to end of line
dd delete current line
J join current line with following line

Going Further

  • :map for mapping a key in command mode to a group of commands. Example: :map de :1,$d^M will delete all lines when using the :de command.
  • :set to define or show your editor options. Example: :set number will make vi display line numbers, and ":set all" will display all current vi options.
  • :ab to define a text abbreviation in insert mode. Example: :ab VIM Vi Improved will auto-complete "VIM" in insert mode for the phrase "Vi Improved".
Other Blog posts that you may like...
The Complete How To Guide of Bash Functions
Learn how to write shell scripts with bash functions. This guide includes examples and best practices on how to define, call, and debug functions in bash.
8 Tips to Get the Most of an Old Windows XP
8 command line tips to get the most value of your old Windows XP, Windows Server 2003, and others.
How To Create Simple Menu with the Shell Select Loop?
The select loop is not a regular shell loop. It can be used in Bash to generate a simple menu from which a user can select numbered options.
What is the Right Way to do Bash Loops?
Looping over a list of numbers or words is a building block in shell scripts. Learn how to write Bash loops, including for loop, while loop, and until loop.