Vim: Exiting Cheat Sheet

:q[uit]Quit VimThis fails when changes have been made.
:q[uit]!Quit without writing
:qaQuit all buffers / tabs
:cq[uit]Quit always, without writing
:wqWrite the current file and exit
:wqaQuit and write all buffers / tabs
: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[!]Write the lines in [range] and exit always
ZZWrite current file, if modified, and exit
ZQQuit current file and exitSame as :q!.

Vim: Editing a File Cheat Sheet

:e[dit]Edit the current fileThis is useful to re-edit the current file, when it has been changed outside of Vim.
:e[dit]!Edit the current file alwaysDiscard 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} alwaysDiscard any changes to the current buffer.
gfEdit the file whose name is under or after the cursorMnemonic: goto file.

Vim: Inserting Text Cheat Sheet

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: Deleting Text Cheat Sheet

<Del>xDelete [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{Visual}dDelete the highlighted textFor {Visual} see Selecting Text.
{Visual}CTRL-H{Visual}<BS>Delete the highlighted text when in Select mode
{Visual}X{Visual}DDelete the highlighted lines
:[range]d[elete]Delete [range] linesDefault: current line.
:[range]d[elete] {count}Delete {count} lines, starting with [range]

Vim: Changing / Replacing Text Cheat Sheet

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 rightIf a [count] is given, do that many characters.
g~{motion}Switch case of {motion} text
{Visual}~Switch case of highlighted text
{Visual}UUppercase highlighted text
SHIFT+I<comment-char>ESCComment vertical selectionSelect range in column where comment char should appear with CTRL+V.
xUncomment vertical selectionSelect range in column where comment char should be removed with CTRL+V.

Vim: Ranges Cheat Sheet

A range permits a command to be applied to a group of lines in the current buffer. For most commands, the default range is the current line.

  • :21s/old/new/g – Line 21
  • :1s/old/new/g – First line
  • :$s/old/new/g – Last line
  • :%s/old/new/g – All lines
  • :.,$s/old/new/g – Current line to end

Vim: Substituting Cheat Sheet

For each line in [range] replace a match of {pattern} with {string}

:[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count]

Repeat last :substitute with same search pattern and substitute string, but without the same flags

:[range]s[ubstitute] [c][e][g][r][i][I] [count] :[range]&[c][e][g][r][i][I] [count]

You may add extra flags.

The arguments that you can use for the substitute commands:

  • [c] Confirm each substitution. Vim positions the cursor on the matching string. You can type:
    • y to substitute this match
    • n to skip this match
    • a to substitute this and all remaining matches
    • q to quit substituting
    • CTRL-E to scroll the screen up
    • CTRL-Y to scroll the screen down
  • [e] When the search pattern fails, do not issue an error message and, in particular, continue in maps as if no error occurred
  • [g] Replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line
  • [i] Ignore case for the pattern
  • [I] Don’t ignore case for the pattern
  • [p] Print the line containing the last substitute

Vim: Copying and Moving Text Cheat Sheet

"{a-zA-Z0-9.%#:-"}Use register {a-zA-Z0-9.%#:-“} for next delete, yank or putUse uppercase character to append with delete and yank. {.%#:} only work with put.
:reg[isters]:di[splay]Display the contents of all numbered and named registers
:reg[isters] {arg}:di[splay] [arg]Display the contents of the numbered and named registers that are mentioned in {arg}
["x]y{motion}Yank {motion} text [into register x]
["x]yy["x]YYank [count] lines [into register x]
{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], [into register x]Default: current line.
["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.

Vim: Undo, Redo and Repeat Cheat Sheet

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.
.Repeat last change, with count replaced with [count]

Vim: Moving Around Cheat Sheet

Basic motion commands

  k              <up>
h   l      <left>    <right>
  j             <down>
h<Left>[count] characters to the left (exclusive)
l<Right><Space>[count] characters to the right (exclusive)
k<Up>CTRL-P[count] lines upward
j<Down>CTRL-J<NL>CTRL-N[count] lines downward (linewise)
0<Home>To the first character of the line (exclusive)
^To the first non-blank character of the line
$<End>To the end of the line and [count – 1] lines downward
g0g<Home>To the first character of the screen line (exclusive) when lines wrap (‘wrap on). Differs from 0 when a line is wider than the screenTo the leftmost character of the current line that is on the screen when lines don’t wrap (‘wrap’ off). Differs from 0 when the first character of the line is not on the screen
g^To the first non-blank character of the screen line (exclusive) when lines wrap (‘wrap’ on). Differs from ^ when a line is wider than the screen.To the leftmost non-blank character of the current line that is on the screen when lines don’t wrap (‘wrap’ off). Differs from ^ when the first non-blank character of the line is not on the screen
g$g<End>To the last character of the screen line and [count – 1] screen lines downward (inclusive) when lines wrap (‘wrap’ on). Differs from $ when a line is wider than the screen.To the rightmost character of the current line that is visible on the screen when lines don’t wrap (‘wrap’ off). 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)
+CTRL-M<CR>[count] lines downward, on the first non-blank character (linewise)
_ <underscore>[count] – 1 lines downward, on the first non-blank character (linewise)
CTRL-EndGGoto line [count] on the first non-blank characterDefault: last line.
CTRL-HomeggGoto line [count] on the first non-blank characterDefault: first line.
SHIFT-Rightw[count] words forward
CTRL-RightW[count] WORDS forward
eForward to the end of word [count]
EForward to the end of WORD [count]
SHIFT-Leftb[count] words backward
CTRL-LeftB[count] WORDS backward
geBackward to the end of word [count]
gEBackward to the end of WORD [count]
HMove to the top of the screen
MMove to the middle of the screen
LMove to the bottom of the screen
zzScroll the line with the cursor to the center of the screen
ztScroll the line with the cursor to the top
zbScroll the line with the cursor to the bottom

The following commands move over words or WORDS.

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

A WORD consists of a sequence of non-blank characters, separated with white space. 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 columnWhen 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

Vim: Marks Cheat Sheet

m{a-zA-Z}Set mark {a-zA-Z} at cursor positionDoes not move the cursor, this is not a motion command.
m'm\`Set the previous context markThis 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}:[range]k{a-zA-Z}Set mark {a-zA-Z} at last line number in [range], column 0Default is cursor line.
'{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 in Vim

/{pattern}[/]<CR>Search forward for the [count]’th occurrence of {pattern}
/{pattern}/{offset}<CR>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 newIf {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
:lv {pattern} [g][j] {file(s)}Search in files with internal grep
Results are put into “quickfix list” open with :cw
+ ‘g’ return all matches not just one per line
+ ‘j’ don’t jump to first match automatically
+ for recursive search use ** file pattern like **/*.c

Vim: Selecting Text (Visual Mode) Cheat Sheet

To select text, enter visual mode with one of the commands below, and use motion commands to highlight the text you are interested in.

Then, use some command on the text.

The operators that can be used are:

  • ~ switch case
  • d delete
  • c change
  • y yank
  • > shift right
  • < shift left
  • ! filter through an external command
  • = filter through ‘equalprg’ option command
  • gq format lines to ‘textwidth’ length
vStart Visual mode per character
VStart Visual mode linewise (horizontal selection)
CTRL-VStart Visual mode columnwise (vertical selection)
<Esc>Exit Visual mode without making any changes
viwVisually select inner word (where the cursor is positioned)

Vim: Suspending Cheat Sheet

CTRL-ZSuspend VimLike :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][!]: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!

Vim: Multiple Windows Cheat Sheet

:e filenameEdit another file
:split filenameSplit window and load another file
CTRL-W vVertical split current window
CTRL-W sHorizontal split current window
CTRL-W Arrow UpMove cursor up a window
CTRL-W CTRL-WMove cursor to another window (cycle)
CTRL-W_Maximize current window
CTRL-W=Make all equal size
10 CTRL-W+Increase window size by 10 lines
:vsplit fileVertical split
:sview fileSame as split, but readonly
:hideClose current window
:onlyKeep only this window open
:lsShow current buffers
:b 2Open buffer #2 in this window
:bd[n]Close current bufferIf [n] is given close buffer #n#n can be gathered with :ls.

Vim: Browsing with Tags Cheat Sheet

When editing programs, there is often a need to jump to another location. Vim uses a tags file that lists each word and location. The tags file has to be created by a utility able to handle the syntax of your files, and has to be updated after significant editing has occurred.

CTRL-]LMB-on-tag+CTRLg LMB-on-tagJump to tagPostion the cursor over a tag name, visually select text, use the left-mouse-button (LMB). Pushes current location to tag stack for later return.
CTRL+tReturn after jumpPops location to jump to from tag stack.
:tagsShow tag stackList of return locations, used by CTRL+t.

Vim: Internal Lists Cheat Sheet

  • Vim has one global quickfix list. The list contains file positions filled by other commands.
  • Vim has one location list per window. The list is similar to a quickfix list and contains a list of positions in filesi.
:cwOpen “quickfix list” (one global list)
:cclClose “quickfix list”
:lwOpen “location list” (one per window)