Published on

Vim Cheat Sheet

This Vim cheat sheet provides a quick overview of all the basic commands. It can’t cover every edge case, so if you need more information about any of these elements, refer to the documentation.

Commands

CommandDescription
:qQuit (fails if there are unsaved changes).
:q!Quit and throw away unsaved changes.
:wWrite (save) the file.
:wqWrite (save) the file and exit.
:xWrite (save) the file and exit.

Cursor Movement

CommandDescription
HMove to top of screen.
MMove to middle of screen.
LMove to bottom of screen.
wJump forwards to the start of a word.
WJump forwards to the start of a word (words can contain punctuation).
eJump forwards to the end of a word.
EJump forwards to the end of a word (words can contain punctuation).
bJump backwards to the start of a word.
BJump backwards to the start of a word (words can contain punctuation).
0Jump to the start of the line.
^Jump to the first non-blank character of the line.
$Jump to the end of the line.
GGo to the last line of the document.
<number>GGo to line number (e.g. 5G goes to line 5).

Inserting/Appending Text

CommandDescription
iInsert before the cursor
IInsert at the beginning of the line
aAppend (insert) after the cursor
AAppend (insert) at the end of the line
oOpen (append) a new line below the current line
OOpen (append) a new line above the current line
eaAppend (insert) at the end of the word
ESCExit insert mode

Copy and Paste

CommandDescription
yyYank (copy) a line.
<number>yyYank (copy) a number of lines (e.g. 2yy copies 2 lines).
ywYank (copy) the characters of the word from the cursor position to the start of the next word.
y$Yank (copy) to end of line.
pPut (paste) the clipboard after cursor.
PPut (paste) before cursor.
ddDelete (cut) a line.
<number>ddDelete (cut) a number of lines lines (e.g. 2dd deletes 2 lines).
dwDelete (cut) the characters of the word from the cursor position to the start of the next word.
DDelete (cut) to the end of the line.
d$Delete (cut) to the end of the line.
xDelete (cut) character.

Configure VIM for shell scripting

  • syntax: on or :set syntax=sh - turns on syntax highlighting.

    Note: For this feature to work the file you are editing must have a shebang indicating the file is a shell script (e.g. #!/bin/bash).

  • :set hlsearch - turns on the highlight search results.

  • :set tabstop=4 - the value to 4 (the default is 8).

  • :set autoindent - causes VIM to indent a new line the same amount as the line just typed.