Skip to content

Vim Commands

  • + — first non-blank character of the next line
  • 0 — start of the line
  • $ — end of the line
  • G — last line of the file
  • 1G, gg, :1 or 1gg — first line of the file
  • 2G — line 2; <n>G — line n
  • /word — search forward for word; n repeats the search
  • ggVG — select the whole file: gg moves to the first line, V starts line-wise visual mode, G extends the selection to the last line
  • i — insert before the cursor
  • a — insert after the cursor
  • A — insert at the end of the line
  • o — open a new line below the cursor and insert
  • Go — jump to the last line, then open a new line below it: insert at the end of the file

While in insert mode, Ctrl-o runs exactly one normal-mode command and then returns to insert mode — useful for a single motion without abandoning the edit in progress.

  • x — delete the character under the cursor
  • dw — delete to the start of the next word
  • dt" — delete forward up to, but not including, the next "
  • di" — delete everything inside the quotes
  • da" — delete everything inside the quotes, including the quotes themselves
  • d$ — delete from the cursor to the end of the line
  • dG — delete from the cursor to the end of the file
  • u — undo; Ctrl-r — redo

di and da work with any delimiter pair — di(, da{, di< — and with w, s and p for word, sentence and paragraph. Substituting c for d deletes and enters insert mode.

  • :s/toReplace/withWord — first match on the current line
  • :%s/toReplace/withWord — first match on every line of the file
  • :3,4s/toReplace/withWord — first match on lines 3 to 4

Append g to replace every match on each line rather than the first, and c to confirm each replacement.

  • vim +42 myfile.txt — open positioned on line 42
  • vim + myfile.txt — open positioned on the last line, in normal mode. The space matters: + followed immediately by text is read as an Ex command to run rather than as a line number, so vim +myfile.txt opens an empty buffer and fails
  • vim +/pattern myfile.txt — open positioned at the first match
  • vim "+set number" myfile.txt — run an Ex command after the file loads
  1. Press Esc
  2. Press Ctrl-v to start visual-block mode
  3. Select the lines
  4. Press I
  5. Type the comment character
  6. Press Esc — the character is inserted at the start of every selected line
  1. Press Esc
  2. Press Ctrl-v
  3. Select the comment characters
  4. Press x
  5. Press Esc
  1. Go to the first line
  2. Press V
  3. Move down to the last line
  4. Press d