Vim Keyboard Cheat Sheet



Vim Cheat Sheet Outline

For Those Just Getting Started

Command Mode: Also named Normal Mode, this mode is used to type Vim commands such as those you’ll find in our Vim Cheat Sheet. To exit insert mode and enter command mode, hit the Esc key of your keyboard. Visual Mode: Similar to command mode, but used to highlight areas of text. Normal commands are run on the highlighted area, which, for. Interactive cheat sheet for vi text editors, notably Vim, describing each key in various modes. Vi/vim cheat sheet normal mode (default) Esc normal mode Esc normal mode. New in vim version 7.x. Keyboard map is qwerty; keys are hidden if unassigned by default default style is light.

Vim Keyboard Cheat Sheet 2019

Beginner

The goal with these seven sections is to give the beginner confidence to replace existing tools with the Vim editor.

A Great Vim Cheat Sheet I’ve compiled a list of essential Vim commands that I use every day. I have then given a few instructions on how to make Vim as great as it should be, because it’s painful without configuration. In - (insert) VISUAL - or - (insert) SELECT - will change to the other mode. In - (insert) SELECT - will toggle to - (insert) VISUAL -, and will then toggle back. So: if you start with - (insert) SELECT - you can use to toggle back and forth.

  1. Vim Modes - Navigating modes
  2. Vim Command Structure - Counts, Operators and Motions
  3. Vim Editing - Undo, replace, delete and append
  4. Vim Cut, Copy and Paste - Edit in Visual and Normal Modes
  5. Vim Search and Replace - Edit in Normal and Command Modes
  6. Vim Command Mode - Save, quit, write, read
  7. Vim Registers - Reuse saved blocks of text
  8. Vim Tabs - Manage multiple files in tabs

Accepting that Vim has over 1,000 commands, we only had space to cover about 80 essential commands, but these should provide a good base of knowledge to launch a further exploration of Vim commands.

Vim and Vi Version Confusion

On most modern Linux distributions for command-line operation, Vim is the replacement for the Vi editor, despite being launched with the command vi. If this is the case for you, your system is most-likely running a limited version of Vim, called vim-tiny. You can find your version using :ve inside Vim. If it says 'Small version without GUI' then you have a limited, but small program. Many of the commands covered in this Vim cheat sheet will not work for you as vim-tiny emulates vi commands and vi shortcuts, so keep that in mind.

Sheet

If that is the case for you, and you can upgrade to the full version of Vim, called vim-runtime, look for a tutorial on this later.

Vim Modes

Keystrokes have alternative meanings in each mode. So a vital first step is knowing your current mode and how to navigate between them.

  • Normal Mode - navigating and some editing
  • Insert Mode - editing and appending text
  • Visual Mode - selecting and moving text
  • Command Mode (Ex Mode) - entering Vim commands

The default or home mode is Normal Mode and Esc returns you here.

Navigating Vim Modes

Keystrokes used to navigate between Vim modes.

Current ModeDesired ModeCharacter(s)
Normal ModeInsert ModeA, a, C, I, i, O, o, S, s
Normal ModeCommand Mode (Ex Mode):
Normal ModeVisual Modev
Any ModeNormal ModeEsc (sometimes twice)
Vim keyboard cheat sheet for beginners

So if you make unintended keystrokes, return to Normal Mode using Esc. There are other ways to navigate between modes of course, and here we cover the basics for beginners.

Vim Command Structure

Counts, Operators and Motions

A combined command is one that can be applied multiple times, taking a variety of actions in all directions.

  • Counts - The number of times to repeat an Operator or Motion, often abbreviated as N
  • Operators - Actions or Vim commands to perform
  • Motions - A navigational direction to move the cursor

Below is a short example of three combined commands translated into Counts, Operators and Motions.

Combined commandCountOperatorMotionWhat does it do?
5dw5dwDelete 5 words
3j3jMove down 3 lines
3g~l3g~lSwap case for next 3 characters
Counts

All Counts 1, 2, 3, 4, 5, 6, 7, 8, 9 require an Operator and/or Motion afterwards. Use Esc to erase an unwanted Count. Counts are optional, and without a Count you can think of the default being 1.

0 is not a Count, but instead moves the cursor to the first column of a line.

Operators

Of the 15 Operators in total, below are those commonly used by beginners. Operators are optional.

CharacterOperation
cChange
dDelete
g~Swap case
guMake lowercase
gUMake uppercase
yYank (copy)
<Shift left
>Shift right
Motions

Vim Motions can optionally be preceded by a Count and/or an Operator. Vim will move or take action from the cursor a specified number of times and remain in Normal Mode. Also note, there are many ways (over 90) to initiate a Motion but to save time and to avoid feeling overwhelmed, start with those in the first column.

CharacterSynonym(s)MotionUnits
hBackspace, Ctrl-hLeftCharacters
lSpaceRightCharacters
jEnter, Ctrl-j, Ctrl-m, Ctrl-nDownLines
kCtrl-pUpLines
bBackwardWords
wForwardWords
{BackwardParagraphs
}ForwardParagraphs
Ctrl-dDown1/2 Screens
Ctrl-uUp1/2 Screens
Ctrl-bBackwardScreens
Ctrl-fForwardScreens

The Ctrl-s keystroke is used for Linux Terminal control and may lock your Vim console. If this happens, hit Ctrl-q to regain control.

Other Motions

The following are important and commonly-used Motions.

CharacterPrefix with Count?Motion
0 (zero)NoMove cursor to beginning of current line
$YesMove cursor to end of current line
^NoMove cursor to first non-blank character of current line
g_YesMove cursor to last non-blank character of current line
ggNoGo to start of file
GNoGo to end of file
HNoGo to top of screen
LNoGo to bottom of screen
MNoGo to middle of screen
zzNoCenter the screen around the cursor

A Count plus the vertical bar or pipe will go to that column of the current line. So 80| will go to column 80.

Vim Editing

Two Modes for Editing: Normal Mode and Insert Mode

Here we cover editing files from two modes. Light edits are performed while remaining in the default Normal Mode, while others such as appending text take a Motion followed by a change to Insert Mode.

Normal Mode Vim Editing
CharacterPrefix with Count?Meaning
uYesUndo the last edit
Crtl-rYesRedo last undo changes
rYesReplace one or several characters
xYesDelete character under cursor and forward
.YesRepeat the last change at the cursor

The r command is recommended for beginers over R which kicks you into a whole different mode called Replace Mode. It is often easier to use Insert Mode to edit larger blocks of text, rather than learn a new mode.

Vim Graphical Cheat Sheet German Keyboard Layout

Insert Mode Vim Editing
CharacterPrefix with Count?Meaning
aYesAppend text after cursor position
AYesAppend text at end of line
CYesCut text from cursor to end of line
IYesInsert text at the first non-blank character of the line
iYesInsert text before the cursor
OYesOpen line(s) above cursor
oYesOpen line(s) below cursur
SYesSwap or delete line(s) for newly created line
sYesSwap or delete character(s) for newly type characters

Vim Cut, Copy and Paste

Two Modes: Visual Mode and Normal Mode

Here are two ways to select blocks of text to accomplish standard cut, copy and paste operations. First, and easiest for those coming from other visual editors, is in Visual Mode. Second is remaining in Normal Mode.

Visual Mode

The following keystrokes perform the most basic copy and paste, switching from Normal Mode to Visual Mode.

CharacterSynonymMeaning
vSwitch to Visual Mode for character selection
yYank (copy) text selected with Motions
pPut (paste) selected text block after the cursor
xdDelete a block of selected text
Ctrl-v, (select rows), Shift+i, (add text), EscThis enters Visual Block mode and is helpful for adding text or comment characters in front of multiple lines.

Once you enter Visual Mode text is highlighted as you select blocks of text with standard Motions. After you are happy with the block, type your cut or copy command from the table above. You will then automatically return to Normal Mode to paste the text.

Many find Visual Mode easier to grasp at first but advancing to Normal Mode for cut, copy and paste operations can save keystrokes.

The Visual Block Mode example above can be a timesaver if for example you wanted to comment out 20 lines of code in Python with the hash # character. Start at the first line where you want to enter a comment and hit Ctrl+v, then navigate down the 19 lines, hit Shift+i and input the text you would like, here a # and space, then Esc.

Normal Mode

To perform cut, copy and paste operations from within Normal Mode requires a good understanding of Motions.

CharacterPrefix with Count?Meaning
y$YesYank (copy) to end of line
yyYesYank (copy) the whole current line
ywYesYank (copy) current word
YYesYank (copy) the whole current line
PYesPut (paste) before the cursor
pYesPut (paste) after the cursor

Vim Search and Replace

Search Lines, Files and Replace Text

Vim offers many commands to search for and replace text at the line-level and file-level.

We will stick with the basics here, but at a more advanced level, with regular expressions, the Vim editor offers the ability initiate any search imaginable, even across whole filesystems.

Search Lines

Searching for characters within lines can be helpful for jumping to specific coding symbols and letters.

CharacterPrefix with Count?Meaning
f{character}YesSearch current line right for N occurance of {character}
F{character}YesSearch current line left for N occurance of {character}
Search Files

File-level search is one of the most common functions for Vim users, and most users memorize one of the two pattern recognition commands because when combined with the n and N symbols noted above, they both can direct Vim to search up and down a file just as easily, so using / or ? is your preference.

CharacterPrefix with Count?Meaning
/{pattern}ReturnYesSearch for next N occurance of {pattern}
?{pattern}ReturnYesSearch for previous N occurance of {pattern}
nYesSearch for next N occurance of an already initiated search in the same direction
NYesSearch for next N occurance of an already initiated search in the opposite direction
:noh<Enter>NoTo turn off highlighting of searched-for text.
Search and Replace

Here we start to see entries in Command Mode using the : character. The term Command Mode, often confuses beginners because you can enter Vim commands from any mode. Official Vim documentation refers to commands entered with : as Ex Commands, but most people use the term Command Mode.

Character(s)Meaning
:%s/{search}/{replace}/gcSearch over the range (% for whole file) for all occurances of {search} and replace with {replace}.
:10,20s/{search}/{replace}/gcSearch over the range (lines 10 to 20) for all occurances of {search} and replace with {replace}.

The only spaces allowed in the string above sit between the / characters, and the { and } characters are not typed. The g means globally over the whole file and c instructs Vim to provide a confirmation before each replacement is made.

A confirmation dialog will prompt you for one of seven choices.

  1. y - substitute at current stop
  2. n - skip this substitution
  3. a - accept all future substitutions
  4. q - quit the search and replace
  5. l - substitute at current stop then quit
  6. ^E (Ctrl-e) - scroll up to previous stop
  7. ^Y (Ctrl-y) - scroll down to next stop

If you leave the c off the end of the command, Vim will perform all replacements in the whole file almost instantly, so it is best for beginners to start with confirming changes.

A Search and Replace Example

The following command uses Regular Expressions to remove the last one or several blank spaces at the end of each line in a file :%s/s+$//.

Vim Command Mode

Save, Quit, Write and Read

Many essential commands sit in Command Mode, sometimes called Ex Command Mode. All of the following commands are input from Normal Mode, so from any other mode, hit Esc once (sometimes twice) to return to Normal Mode. When entering Command Mode, once the colon character : is pressed, your additional keystrokes appear at the bottom left of the screen.

Vim Basics Cheat Sheet

There are as many as 515 commands in Command Mode and many can be abbreviated. Also, unlike with other modes, commands here, because they are open-ended, require Enter to complete.

Character(s)Synonym(s)Meaning
:w:writeWrite (save) current and previously-named file.
:w {file}:write {file}Write (save) current file and name it {file}.
:wq:x, ZZWrite (save) current and previously-named file and quit Vim.
:q:quitQuit Vim from an unedited file.
:q!ZQQuit Vim from an edited or unedited file without saving changes.
:e {file}:edit {file}Edit a file named {file}.
:r {file}:read {file}Read an external file named {file} at the current cursor position.
:sh:shellTo leave Vim temporarily and go to the shell. Type exit to return.
:!{cmd}To run a shell command {cmd}. Hit q to return to Vim.
:h:helpOpen help in a new window. Use :q to quit help.

Vim Registers

Vim Registers allow you to save blocks of code to paste anywhere you like. Functionality is much like cut, copy and paste operations elsewhere but Registers allow you to save many blocks, using letters a-z and A-Z, so you have 52 slots. The last 10 yanks and deletes are automatically saved under the 0-9 slots in the Register. These can be viewed with :reg.

Creating, pasting and deleting Registers

Registers work logically with actions in Normal Mode and Visual Mode. The ' symbol starts the Register functionality. Examples below use the empty Register name c.

Character(s)Synonym(s)Meaning
'cyyTo save a Register from Normal Mode under the name c of a whole line.
'cyTo save a Register from Visual Mode under the name c of a selected section.
'cpTo put (paste) text from the saved Register named c while in Normal Mode.
:call setreg('c', [])To delete an item from the Register replace it with empty text.
:reg:registersView the currently saved Registers. Type q to quit.
:h reg:help registersFind help on Registers. Type :q to quit.

Besides assigning Registers to letters and numbers, an additional 8 types exist for more advanced uses. These are described in help.

Vim Tabs

You can manage multiple text files in tabs within one Vim window. To navigate between tabs click tab titles with the mouse or use the keystrokes described below. The 'X' at the top right when clicked will close that tab.

Character(s)Synonym(s)Meaning
:tabe:tabedit
:tabnew
To open a new blank tab.
:tabe {file}:tabedit {file}
:tabenew {file}
To open a new tab to edit {file}.
:tabc:tabclose
:tabclose!
To close the current tab.
{count} gtTo specify which tab with {count} or go to the next tab. This wraps around forward.
{count} gTTo specify which tab with {count} or go to the previous tab. This wraps around backward.
:tabsList the current Vim tabs.

Additional Vim tab functionality includes reordering tabs, looping over tabs and closing all tabs. See :help tabs for more.

Related Content

  • A similarly organized Linux Cheat Sheet
  • Practice hjkl navigation with the Vimazing Race Maze Game
  • Common Vim commands with videos in the Vim Reference

How to Exit

:q[uit]Quit Vim. This fails when changes have been made.
:q[uit]!Quit without writing.
:cq[uit]Quit always, without writing.
:wqWrite the current file and exit.
:wq!Write the current file and exit always.
:wq {file}Write to {file}. Exit if not editing the last
:wq! {file}Write to {file} and exit always.
:[range]wq[!][file] Same as above, but only write the lines in [range].
ZZWrite current file, if modified, and exit.
ZQQuit current file and exit (same as ':q!').
Vim keyboard shortcuts cheat sheet

Editing a File

:e[dit]Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim.
:e[dit]!Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again.
:e[dit] {file}Edit {file}.
:e[dit]! {file}Edit {file} always. Discard any changes to the current buffer.
gfEdit the file whose name is under or after the cursor. Mnemonic: 'goto file'.

Inserting Text

aAppend text after the cursor [count] times.
AAppend text at the end of the line [count] times.
iInsert text before the cursor [count] times.
IInsert text before the first non-blank in the line [count] times.
gIInsert text in column 1 [count] times.
oBegin a new line below the cursor and insert text, repeat [count] times.
OBegin a new line above the cursor and insert text, repeat [count] times.
Vim cheat sheet for programmer

Inserting a file

:r[ead] [name]Insert the file [name] below the cursor.
:r[ead] !{cmd}Execute {cmd} and insert its standard output below the cursor.

Deleting Text

<Del> or
x
Delete [count] characters under and after the cursor
XDelete [count] characters before the cursor
d{motion}Delete text that {motion} moves over
ddDelete [count] lines
DDelete the characters under the cursor until the end of the line
{Visual}x or
{Visual}d
Delete the highlighted text (for {Visual} see Selecting Text).
{Visual}CTRL-H or
{Visual}
When in Select mode: Delete the highlighted text
{Visual}X or
{Visual}D
Delete the highlighted lines
:[range]d[elete]Delete [range] lines (default: current line)
:[range]d[elete] {count}Delete {count} lines, starting with [range]

Changing (or Replacing) Text

r{char}replace the character under the cursor with {char}.
REnter Insert mode, replacing characters rather than inserting
~Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters.
~{motion}switch case of {motion} text.
{Visual}~Switch case of highlighted text

Substituting

:[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count]For each line in [range] replace a match of {pattern} with {string}.
:[range]s[ubstitute] [c][e][g][r][i][I] [count] :[range]&[c][e][g][r][i][I] [count]Repeat last :substitute with same search pattern and substitute string, but without the same flags. You may add extra flags

Copying and Moving Text

'{a-zA-Z0-9.%#:-'}Use register {a-zA-Z0-9.%#:-'} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put).
:reg[isters]Display the contents of all numbered and named registers.
:reg[isters] {arg}Display the contents of the numbered and named registers that are mentioned in {arg}.
:di[splay] [arg]Same as :registers.
['x]y{motion}Yank {motion} text [into register x].
['x]yyYank [count] lines [into register x]
['x]Yyank [count] lines [into register x] (synonym for yy).
{Visual}['x]yYank the highlighted text [into register x] (for {Visual} see Selecting Text).
{Visual}['x]YYank the highlighted lines [into register x]
:[range]y[ank] [x]Yank [range] lines [into register x].
:[range]y[ank] [x] {count}Yank {count} lines, starting with last line number in [range] (default: current line), [into register x].
['x]pPut the text [from register x] after the cursor [count] times.
['x]PPut the text [from register x] before the cursor [count] times.
['x]gpJust like 'p', but leave the cursor just after the new text.
['x]gPJust like 'P', but leave the cursor just after the new text.
:[line]pu[t] [x]Put the text [from register x] after [line] (default current line).
:[line]pu[t]! [x]Put the text [from register x] before [line] (default current line).

Undo/Redo/Repeat

uUndo [count] changes.
:u[ndo]Undo one change.
CTRL-RRedo [count] changes which were undone.
:red[o]Redo one change which was undone.
UUndo all latest changes on one line. {Vi: while not moved off of it}
.Repeat last change, with count replaced with [count].

Vim Shortcut Keys Cheat Sheet

Moving Around

h or
[count] characters to the left (exclusive).
l or
or
[count] characters to the right (exclusive).
k or
or
CTRL-P
[count] lines upward
j or
or
CTRL-J or
or
CTRL-N
[count] lines downward (linewise).
0To the first character of the line (exclusive).
<Home>To the first character of the line (exclusive).
^To the first non-blank character of the line
$ or
<End>
To the end of the line and [count - 1] lines downward
g0 or
g<Home>
When lines wrap ('wrap on): To the first character of the screen line (exclusive). Differs from '0' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost character of the current line that is on the screen. Differs from '0' when the first character of the line is not on the screen.
g^When lines wrap ('wrap' on): To the first non-blank character of the screen line (exclusive). Differs from '^' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost non-blank character of the current line that is on the screen. Differs from '^' when the first non-blank character of the line is not on the screen.
g$ or
g<End&gr;
When lines wrap ('wrap' on): To the last character of the screen line and [count - 1] screen lines downward (inclusive). Differs from '$' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the rightmost character of the current line that is visible on the screen. Differs from '$' when the last character of the line is not on the screen or when a count is used.
f{char}To [count]'th occurrence of {char} to the right. The cursor is placed on {char} (inclusive).
F{char}To the [count]'th occurrence of {char} to the left. The cursor is placed on {char} (inclusive).
t{char}Till before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of {char} (inclusive).
T{char}Till after [count]'th occurrence of {char} to the left. The cursor is placed on the character right of {char} (inclusive).
;Repeat latest f, t, F or T [count] times.
,Repeat latest f, t, F or T in opposite direction [count] times.
- <minus>[count] lines upward, on the first non-blank character (linewise).
+ or
CTRL-M or
<CR>
[count] lines downward, on the first non-blank character (linewise).
_ <underscore>[count] - 1 lines downward, on the first non-blank character (linewise).
<C-End> or
G
Goto line [count], default last line, on the first non-blank character.
<C-Home> or
gg
Goto line [count], default first line, on the first non-blank character.
<S-Right> or
w
[count] words forward
<C-Right> or
W
[count] WORDS forward
eForward to the end of word [count]
EForward to the end of WORD [count]
<S-Left> or
b
[count] words backward
<C-Left> or
B
[count] WORDS backward
geBackward to the end of word [count]
gEBackward to the end of WORD [count]
These commands move over words or WORDS.

A word consists of a sequence of letters, digits and underscores, or asequence of other non-blank characters, separated with white space (spaces,tabs, ). This can be changed with the 'iskeyword' option.

A WORD consists of a sequence of non-blank characters, separated with whitespace. An empty line is also considered to be a word and a WORD.

([count] sentences backward
)[count] sentences forward
{[count] paragraphs backward
}[count] paragraphs forward
]][count] sections forward or to the next '{' in the first column. When used after an operator, then the '}' in the first column.
][[count] sections forward or to the next '}' in the first column
[[[count] sections backward or to the previous '{' in the first column
[][count] sections backward or to the previous '}' in the first column

Screen movement commands

z.Center the screen on the cursor
ztScroll the screen so the cursor is at the top
zbScroll the screen so the cursor is at the bottom

Marks

m{a-zA-Z}Set mark {a-zA-Z} at cursor position (does not move the cursor, this is not a motion command).
m' or
m`
Set the previous context mark. This can be jumped to with the '' or '``' command (does not move the cursor, this is not a motion command).
:[range]ma[rk] {a-zA-Z}Set mark {a-zA-Z} at last line number in [range], column 0. Default is cursor line.
:[range]k{a-zA-Z}Same as :mark, but the space before the mark name can be omitted.
'{a-z}To the first non-blank character on the line with mark {a-z} (linewise).
'{A-Z0-9}To the first non-blank character on the line with mark {A-Z0-9} in the correct file
`{a-z}To the mark {a-z}
`{A-Z0-9}To the mark {A-Z0-9} in the correct file
:marksList all the current marks (not a motion command).
:marks {arg}List the marks that are mentioned in {arg} (not a motion command). For example:

Searching

/{pattern}[/]Search forward for the [count]'th occurrence of {pattern}
/{pattern}/{offset}Search forward for the [count]'th occurrence of {pattern} and go {offset} lines up or down.
/<CR>Search forward for the [count]'th latest used pattern
//{offset}<CR>Search forward for the [count]'th latest used pattern with new. If {offset} is empty no offset is used.
?{pattern}[?]<CR>Search backward for the [count]'th previous occurrence of {pattern}
?{pattern}?{offset}<CR>Search backward for the [count]'th previous occurrence of {pattern} and go {offset} lines up or down
?<CR>Search backward for the [count]'th latest used pattern
??{offset}<CR>Search backward for the [count]'th latest used pattern with new {offset}. If {offset} is empty no offset is used.
nRepeat the latest '/' or '?' [count] times.
NRepeat the latest '/' or '?' [count] times in opposite direction.

Selecting Text (Visual Mode)

To select text, enter visual mode with one of the commands below, and usemotion commands to highlight the text you are interestedin. Then, use some command on the text.

vstart Visual mode per character.
Vstart Visual mode linewise.
<Esc>exit Visual mode without making any changes

How to Suspend

CTRL-ZSuspend Vim, like ':stop'. Works in Normal and in Visual mode. In Insert and Command-line mode, the CTRL-Z is inserted as a normal character.
:sus[pend][!] or
:st[op][!]
Suspend Vim. If the '!' is not given and 'autowrite' is set, every buffer with changes and a file name is written out. If the '!' is given or 'autowrite' is not set, changed buffers are not written, don't forget to bring Vim back to the foreground later!
Daniel Gryniewicz / dang@fprintf.net