ViM Cheat Sheet

August 25, 2024 ยท View on GitHub

ViM - vi improved

Key Points

  • vi is the core editor available on every unix system
  • basic knowledge of vi is a core skill for all unix / linux server administrators
  • tonnes of plugins, highly extensible and widely used
  • see my advanced .vimrc for all sorts of trips & tricks, hotkey linting and building, plugins etc.
vim testfile.txt

Starts in command mode. Below are mostly commands to remember since everything in insert mode is typed out literally.

Commands can often be used together in shorthand combinations or prefixed with a number to repeat the operation N times or for N lines.

Lowercase vs Capitals often do the opposite action / direction.

You can run the vimtutor command on unix or inside vim the command :help tutor to get an interactive tutorial.

Shortcuts

KeystrokesDescription
iEnter insert mode to start typing
aEnter insert mode after the current cursor position. Same as right and then i
IEnter insert mode at beginning of line
AEnter insert mode at end of line
yyCopy entire line. 5yy copies the next 5 lines
ddDelete entire line (also copies it in the buffer like yy)
dwDelete until the next word. 3dw or d3w deletes the next 3 words
pPaste last copied character after cursor, or last copied line underneath current line
PPaste last copied line before current line, or last copied character before cursor
cChange a character - deletes the character under the cursor and enters insert mode. Equivalent to x and then i
cwChange word - deletes until the next word and enters insert mode
ccChange line - deletes entire line and enters insert mode to type to replace it
c$ / CChange until end of line - deletes to end of line and enters insert mode
oOpen a new line in insert mode below the current line
OOpen a new line in insert mode above the current line
EscEscape from insert mode back to command mode, or cancel the current combination of keystroke command
xDelete the character under the cursor
uUndo last character change
UUndo all changes to last edited line
Ctrl + RRedo
:help / :help topicOpen the help menu
Ctrl + ]Jumps to the hyperlink (called tag in vim) denoted by |:something |
Ctrl + TGo back from the hyperlink
:qQuit / exit vim
:wq or ZZWrite & quit
:w!Force write
:q!Force Quit, discarding changes
:waWrite all open files
:wqaWrite all open files and quit
h / j / k / lmove cursor left / up / down / right, respectively. Just use the arrow keys on your keyboard instead, seriously
wJump forwards one word. Type 4w to jump forwards 4 words
bJump backwards one word
^ / 0Jump to the start of the line. Remember ^ mean start of line anchor in regex too
$Jump to the end of the line. Remember $ means the end of line anchor in regex too. 2$ takes you to the end of the next line
fSearch forwards on the current line for the next character you type and jumps there. Prefixing with a number will jump to the Nth occurrence
FSearch backwards on the current line for the next character you type and jump there
tSearch forwards until, same as f but stops cursor one character before
TSearch backwards until, same as F, but stops cursor one character after
ggJump to first line of file
GJump to last line of file
10G / :10Jump to 10th line of file
|Jump to column, without a prefix number jumps to start of line
Ctrl + GShow the filename, line number and % through file in the bottom row status line
Ctrl + UJumps cursor up half a page
Ctrl + DJumps cursor down half a page
Ctrl + FMoves the file forward down a page
Ctrl + BMoves the file backwards up a page
.Repeats the last command
JJoins the current and next line
rReplaces the character under the cursor with the next character you type. 5ra replaces the next 5 characters with aaaaa
~Toggles the case of the character under the cursor. Hold to toggle through the letters of the word under the cursor

VimScript Style Guide

https://google.github.io/styleguide/vimscriptguide.xml