Vim Essentials Cheatsheet & Guide

Published:

This is my Vim cheatsheet with all of the most essential commands! You can check out my youtube video for a complete guide on how almost all of these work and how I go about remembering them.

Changing Vim Modes

CommandDescription
iEnter INSERT mode
aEnter INSERT mode after the cursor (think: append)
AEnter INSERT mode at the end of the line (think: Append)
oOpen new line below the cursor and enter INSERT mode
OOpen new line above the cursor and enter INSERT mode
vEnter VISUAL mode
Ctrl-vEnter VISUAL-BLOCK mode
:Enter COMMAND-LINE mode
REnter REPLACE mode
ESCGo back to NORMAL mode from other modes

Exiting

CommandDescription
:wWrite (save) file without exiting
:waWrite (save) all open files without exiting
:qQuit but fail if unsaved changes exist
:q!Quit and discard unsaved changes
:wq or :xWrite (save) and quit
:wqaWrite and quit on all open files

Moving Around Within Vim

Arrows

CommandDescription
hMove cursor left (left most)
jMove cursor down (looks like down arrow)
kMove cursor up
lMove cursor right (right most)

Movements Within A Line

CommandDescription
$Move cursor to the end of the line
0Move cursor to the beginning of the line
^Move cursor to first non-blank character in line
fxFind next occurrence of character ‘x’
FxFind previous occurrence of character ‘x’
txGo towards next occurrence of character ‘x’ (stops right before it)
TxGo towards previous occurence of character ‘x’ (stops right before it)
;Repeat previous f, F, t, or T movement forwards
,Repeat previous f, F, t, or T movement backwards

Word Movements

Definitions:

  • word: Sequence of letters, digits and underscores OR sequence of other symbols, separated by whitespace. Use :h word to learn more.
  • WORD: Any sequence of non-blank characters (any symbols, letters, digits, etc…), separated by whitespace. Use :h WORD to learn more.
CommandDescription
wMove cursor forwards to start of word (sequence of letters, digits, underscores OR sequence of other symbols)
WMove cursor forwards to start of WORD (any sequence of non-blank characters)
bMove cursor backwards to start of word (sequence of letters, digits, underscores OR sequence of other symbols)
BMove cursor backwards to start of WORD (any sequence of non-blank characters)
eMove cursor forwards to end of word (sequence of letters, digits, underscores OR sequence of other symbols)
EMove cursor forwards to end of WORD (any sequence of non-blank characters)
geMove cursor backwards to end of word (sequence of letters, digits, underscores OR sequence of other symbols)
gEMove cursor backwards to end of WORD (any sequence of non-blank characters)

Sentence Movements

Definitions

  • sentence: A sentence ends with a ”.”, ”!” or ”?” followed by the end of the line, a space or tab. Use :h sentence to learn more.
CommandDescription
)Move cursor to next sentence
(Move cursor to previous sentence

Paragraph Movements

Definitions:

  • paragraph: Blocks of consecutive non-empty lines. NOTE: Line with white space is not empty. Use :h paragraph to learn more.
CommandDescription
}Move cursor to next paragraph (block of consecutive non-empty lines)
{Move cursor to previous paragraph (block of consecutive non-empty lines)

Moving To Specific Lines

Note: Replace {number} with an actual number. You can also use numbers in front of other cursor movements like {number}w, {number}b or {number} and many others.

CommandDescription
ggMove cursor to first line of document
GMove cursor to last line of document
{number}GMove cursor to line {number}
{number}jGo {number} lines down
{number}kGo {number} lines up
HMove cursor to line at the top of the window
MMove cursor to the line at the middle of the window
LMove cursor to the line at the bottom of the window

Parenthesis, Bracket, Curly Brace and Method Navigation

CommandDescription
%Find next parenthesis, bracket or curly brace in front of or under the cursor and jump to its match
[(Go to previous unmatched (
[{Go to previous unmatched {
])Go to next unmatched )
]}Go to next unmatched }
]mGo to next start of method (Java like languages)
]MGo to next end of method
[mGo to previous start of method
[MGo to previous end of method

Screen Related Cursor Movements

CommandDescription
Ctrl-FMove cursor forwards one full screen
Ctrl-BMove cursor backwards one full screen
Ctrl-DMove cursor down half a screen
Ctrl-UMove cursor up half a screen

Scrolling While Leaving Cursor In Place

CommandDescription
zzPlace current cursor line in the middle of the window
ztPlace current cursor line at the top of the window
zbPlace current cursor line at the bottom of the window
Ctrl-EScroll down a single line, leaving cursor in place
Ctrl-YScroll up a single line, leaving cursor in place

Search Movements

Note: Use :set ignorecase for case insensitive searching and :set smartcase to override case insensitivity if search pattern has upper case characters.

CommandDescription
/patternSearch forward for pattern
?patternSearch backward for pattern
*Search forward for the word under or in front of the cursor
#Search backward for the word under or in front of the cursor
nRepeat last search in same direction
NRepeat last search in opposite direction

Tip: Use :nohl after a search to temporarily turn off search highlights until another search command is used.

Navigating The Jump List

Context

Certain vim movements that move the cursor several lines away will add entries to the jumplist. You can display the jumplist with :jump.

Common commands that will add entries to the jumplist: G, gg, [number]G, /,?, n, N, %, (, ), {, }, :s, L, M, H. Navigating to a different file/buffer in the same window also works.

IMPORTANT: [number]j and [number]k will not add entries to the jumplist.

For a complete list of commands that add entries to the jumplist use :h jump-motions.

I use this often to navigate from and to different buffers/files in the same window.

CommandDescription
Ctrl-OGo to the previous cursor position in the jump list
Ctrl-IGo to the next cursor position in the jump list

Editing Text

Now that you know how to move within Vim, editing text will go hand in hand with Vim movements.

Note: A {motion} is a Vim command that moves the cursor. Like many of the commands described in the previous section.

Deletion

CommandDescription
d{motion}Delete the text that the {motion} command moves over and copy into register.
ddDelete whole current line and copy into register.
DDelete from under the cursor to the end of the line and copy into register.

Tip: You can also use a {number} before these commands to execute the deletion that {number} of times.

Some Examples

ExampleDescription
dwDelete from cursors current position to start of next word
deDelete from cursors current position to end of word
dGDelete from cursors current position to end of file
d]}Delete from cursors current position to next unmatched }
2ddDelete whole line under cursor and line below it.

The possible combinations are endless…

Undo & Redo

CommandDescription
uUndo last change
Ctrl-RRedo changes that have been undone with u

Tip: You can also use {number} before these undo & redo commands to execute it that {number} of times.

Changing Text

Note: Executing a change command with c is pretty much the same as d except that it takes you into insert mode afterwards.

CommandDescription
c{motion}Delete the text that the {motion} command moves over, copy into register and enter insert mode.
ccDelete whole current line, copy into register and enter insert mode.
CDelete from under the cursor to the end of the line, copy into register and enter insert mode.

Examples would be the same as with delete but changing c to d.

Repeating a File Change

CommandDescription
.Repeat the last change you made to the file

Tip: You can use {number} before . to repeat the change that {number} of times.

This is a really nice one!

Replacing & Deleting Characters

Note: When executing, substitute {character} with an actual character.

CommandDescription
r{character}Replace current character under cursor with {character}
REnter replace mode and start replacing characters by typing until ESC is pressed
xDelete current character under the cursor and copy into register

Tip: You can use {number} before r and x to execute that {number} of times.

Yank (Copy) and Paste (Put)

CommandDescription
y{motion}Yank or copy text that the motion command moves over into register
yyYank or copy whole current line into register
YYank or copy from under the cursor to the end of the line into register
pPut or paste the text found in register (register x) after the cursor
PPut or paste the text found in register (register x) before the cursor

Tip: You can use {number} before y or p to repeat the yank (copy) or put (paste) command that {number} of times.

Note: Commands such as d, c and x mentioned above also copy text into a register. These as well as the y command copy into register x by default.

Changing Case

CommandDescription
~Switch case of character under cursor and move cursor to the right
~{motion}Switch the case of the text that the {motion} command moves over
gu{motion}Change the text that the {motion} command moves over to lowercase
guuMake whole current line lower case
gU{motion}Change the text that the {motion} command moves over to uppercase
gUUMake whole current line upper case

Search/Replace

CommandDescription
:%s/old/new/gReplace all occurrences of “old” with “new” in whole file
:%s/old/new/gcReplace all occurrences of “old” with “new” in whole file, asking for confirmation
:%s/old/new/giReplace all occurrences of “old” with “new” in whole file, ignoring case

Working With Text Objects and Inside/Around 🚀

I personally love using this feature!

You can use Vim text objects to execute an operator on that object or select it with Visual mode (Use v to enter visual mode).

First, here’s a list of handy text objects to remember.

Text Objects To Remember

ObjectDescription
a"A double quoted string, including the quotes
i"A double quoted string, excluding the quotes
a'A single quoted string, including the quotes
i'A single quoted string, excluding the quotes
a( or a)A block surrounded by parenthesis, including the parenthesis
i( or i)A block surrounded by parenthesis, excluding the parenthesis
a[ or a]A block surrounded by brackets, including the brackets
i[ or i]A block surrounded by brackets, excluding the brackets
a{ or a}A block surrounded by curly braces, including the curly braces
i{ or i}A block surrounded by curly braces, excluding the curly braces
a< or a>Text surrounded by <>, including the opening < and the closing >
i< or i>Text surrounded by <>, excluding the opening < and the closing >
atA block surrounded by xml/html tags, including the tags
itA block surrounded by xml/html tags, excluding the tags
awA word including the surrounding whitespace
iwA word excluding the surrounding whitespace
apA paragraph including the surrounding whitespace
ipA paragraph excluding the surrounding whitespace

How to use text objects

To use text objects, place the cursor anywhere inside one and type an {operator} followed by the {text object} to execute the operator on that object.

Tip: Think of ‘a’ as around and ‘i’ as inside.

Examples

CommandDescription
diwDelete word that cursor is in, keeping surrounding whitespace (Think: “delete inside word”)
dawDelete word that cursor is in as well as surrounding whitespace (Think: “delete around word”)
di(Delete everything within parenthesis surrounding cursor, keeping the surrounding parenthesis (Think: “delete inside parenthesis”)
da(Delete everything within parenthesis surrounding cursor as well as the surrounding parenthesis (Think: “delete around parenthesis”)
di"Delete everything within double quotes surrounding cursor, keeping the surrounding double quotes (Think: “delete inside double quotes”)
da"Delete everything within double quotes surrounding cursor as well as the surrounding double quotes (Think: “delete around double quotes”)
dit"Delete everything within tags surrounding cursor, keeping the surrounding tags (Think: “delete inside tags”)
datDelete everything within tags surrounding cursor as well as the surrounding tags (Think: “delete around tags”)

Of course these examples from above can be applied to other operators like c or y for changing, copying, etc… or using v instead of an operator to select the text object in visual mode.

Indentation

CommandDescription
>{motion}Indent text that the {motion} command moves over, to the right
>>Indent whole current line to the right

Tip: You can use {number} before > and >> to execute the indentation that {number} of times. For example, use 2>> to indent the current line and the line below it. Tip # 2: You can also use text objects with >