Vim Cheat Sheet (with Vi Exceptions)

Vim is a powerful text editor that evolved from the classic Vi editor. While most commands are similar, some advanced features in Vim are not present in pure Vi. Below, you’ll find an overview of essential commands—grouped by function—and notes on which ones don’t apply to Vi.


Exiting Vim

One of the first things you should learn is how to properly exit Vim.

  • :q! – Quits without saving changes.
  • :wq or ZZ – Saves the current file and quits.
  • :x – Saves the file and quits only if changes were made.

Basic Commands

These commands are fundamental to starting, saving, and quitting in both Vim and Vi.

  • vim filename – Opens a file with Vim.
  • vi filename – Opens a file with Vi.
  • :q – Quits the editor (will warn if there are unsaved changes).
  • :q! – Quits without saving changes.
  • :w – Saves (writes) the file.
  • :wq or ZZ – Saves and then quits.
  • :x – Saves and quits only if changes were made.

Status Bar and Line Numbers

Vim allows you to configure your status bar and line numbers, making navigation easier. Pure Vi may not support all of these commands.

  • :set ruler – Displays the current cursor position in the status bar.
  • :set laststatus=2 – Always shows the status line.
  • :set number – Shows absolute line numbers.
  • :set relativenumber – Shows line numbers relative to the current cursor line.

Modes in Vim

Vim (and Vi) operate in distinct modes. Understanding these modes is crucial for efficient editing.

  • Normal Mode (press Esc to return to it): Use commands to navigate and manipulate text.
  • Insert Modes:
    • i – Starts inserting text before the cursor.
    • a – Starts inserting text after the cursor.
    • I – Inserts at the beginning of the current line.
    • A – Inserts at the end of the current line.
    • o – Opens a new line below the current one and starts insert mode.
    • O – Opens a new line above the current one and starts insert mode.
  • Visual Modes (Vim-only):
    • v – Visual selection mode.
    • V – Visual line mode.
    • Ctrl+V – Visual block mode (not available in Vi).

Use the following commands in Normal mode to move around quickly:

  • h – Move left.
  • l – Move right.
  • j – Move down.
  • k – Move up.
  • 0 – Jump to the beginning of the current line.
  • ^ – Jump to the first non-blank character in the line.
  • $ – Jump to the end of the current line.
  • w – Move forward to the beginning of the next word.
  • b – Move backward to the beginning of the previous word.
  • e – Move to the end of the current or next word.
  • gg – Go to the beginning of the file (not available in Vi; use 1G instead).
  • G – Go to the end of the file.
  • nG – Go to line number n.
  • H, M, L – Move the cursor to the top, middle, or bottom of the screen, respectively.
  • % – Jump to the matching bracket/parenthesis (not available in Vi).

Editing Text

Whether you’re making small changes or larger edits, these commands will help you manipulate text:

  • x – Deletes the character under the cursor.
  • X – Deletes the character before the cursor.
  • dd – Deletes (cuts) the current line.
  • yy – Copies (yanks) the current line.
  • p – Pastes text after the cursor.
  • P – Pastes text before the cursor.
  • r<char> – Replaces the character under the cursor with <char>.
  • R – Enters Replace mode; typed characters overwrite existing text.
  • u – Undoes the last change.
  • Ctrl+R – Redoes the last undone change (not available in Vi).
  • . – Repeats the last command.

Searching

Vim features powerful search capabilities:

  • /pattern – Searches forward for “pattern.”
  • ?pattern – Searches backward for “pattern.”
  • n – Repeats the last search in the same direction.
  • N – Repeats the last search in the opposite direction.
  • * – Searches forward for the word under the cursor (Vim-only).
  • # – Searches backward for the word under the cursor (Vim-only).

Substitution (Search and Replace)

Use these commands in Command mode for find-and-replace tasks:

  • :s/old/new/ – Replaces the first occurrence of “old” with “new” in the current line.
  • :s/old/new/g – Replaces all occurrences in the current line.
  • :%s/old/new/g – Replaces all occurrences of “old” with “new” throughout the file.
  • :%s/old/new/gc – Does the same but asks for confirmation before each replacement.

Working with Multiple Files

Vim can manage multiple files (buffers) and split windows:

  • :e filename – Opens another file for editing in the current window.
  • :bn – Switches to the next buffer.
  • :bp – Switches to the previous buffer.
  • :bd – Closes (deletes) the current buffer.
  • :split filename – Splits the current window horizontally and opens filename.
  • :vsplit filename – Splits the current window vertically and opens filename.
  • Ctrl+w – Used with arrow keys or hjkl to switch between splits.

Marks and Registers

Vim (and Vi) maintain registers to store text and marks for quick navigation:

  • m<char> – Sets a mark named <char>.
  • '<char> – Jumps to the specified mark.
  • "<char>p – Pastes from register <char>.

Advanced Features

Copy and Cut Operations

  • yw – Yanks (copies) a word from the cursor to the end of the word.
  • yy – Yanks an entire line.
  • y$ – Yanks from the cursor to the end of the line.
  • d$ – Deletes from the cursor to the end of the line.
  • d0 – Deletes from the cursor to the beginning of the line.

Indentation

  • >> – Shifts (indents) the current line to the right.
  • << – Shifts (unindents) the current line to the left.
  • = – Automatically indents a selection (e.g., in Visual mode).

Macros

Macros allow you to record a sequence of commands and replay them:

  • q<char> – Starts recording a macro into register <char>.
  • q – Stops recording.
  • @<char> – Executes the recorded macro.
  • @@ – Repeats the last executed macro.

Command History

  • q: – Opens the command-line history window.
  • q/ – Opens the search history window.

Spell Checking (Vim-only)

  • :set spell – Enables spell checking.
  • ]s – Moves to the next misspelled word.
  • [s – Moves to the previous misspelled word.
  • z= – Suggests spelling corrections for a highlighted misspelled word.

Tabs

  • :tabnew – Opens a new tab.
  • gt – Switches to the next tab.
  • gT – Switches to the previous tab.
  • :tabclose – Closes the current tab.

Vi Exceptions

Because Vim extends Vi, there are certain features not available in a pure Vi environment:

  1. Visual mode (v, V, Ctrl+V) is not supported in Vi.
  2. gg (go to top of the file) is absent; use 1G instead.
  3. % (jump to matching bracket) isn’t available in Vi.
  4. Ctrl+R (redo) isn’t available in Vi.
  5. * and # (search for the word under the cursor) aren’t available in Vi.
  6. Block selection (Ctrl+V) isn’t available in Vi.
  7. Some advanced buffer and window commands may also not work in pure Vi.


This cheat sheet provides an overview of essential Vim commands while pointing out key differences from the original Vi editor. Whether you’re new to modal text editing or looking to refine your existing skills, mastering these commands will help you navigate and edit files more efficiently in both Vim and Vi (where supported).