Vim Editor

Vim cheatsheet — modal text editor commands and keybindings. i insert, :wq save quit, /search, dd delete line, yy yank, p paste, visual mode, macros. Vim survival guide.

9 min read

What it is

Vim is a modal text editor that allows for efficient text manipulation through a command-line interface, ideal for programmers and system administrators who spend a lot of time editing code or configuration files.

Installation

Linux

sudo apt update && sudo apt install vim  # Debian/Ubuntu
sudo yum install vim                   # CentOS/Fedora/RHEL
sudo dnf install vim                   # Fedora

macOS

brew install vim

Windows

Vim is often included with Git for Windows. You can also download installers from the official Vim website or use package managers like Chocolatey:

choco install vim

Core Concepts

  • Modes: Vim has several modes, the most important being:

    • Normal Mode: The default mode. Used for navigation and executing commands. Press Esc to return to Normal Mode from any other mode.
    • Insert Mode: For typing text. Press i (insert before cursor), a (append after cursor), I (insert at beginning of line), or A (append at end of line) to enter Insert Mode.
    • Visual Mode: For selecting text. Press v (character-wise), V (line-wise), or Ctrl+v (block-wise) to enter Visual Mode.
    • Command-Line Mode: For executing ex commands (like :w, :q). Press : to enter Command-Line Mode.
  • Motions: Commands that move the cursor (e.g., w to next word, $ to end of line).

  • Text Objects: Selections based on semantic units (e.g., iw for inner word, ap for a paragraph).

  • Operators: Actions that affect text (e.g., d for delete, c for change, y for yank/copy). Operators are often combined with motions or text objects.

Commands / Usage

Entering and Exiting Vim

  • vim <filename>: Open or create a file.
  • vim: Open Vim without a file.
  • :q: Quit (only if no changes have been made).
  • :q!: Quit without saving changes.
  • :w: Save (write) changes.
  • :wq: Save and quit.
  • :x: Save and quit (similar to :wq, but only writes if changes were made).
  • :qa: Quit all open windows/buffers.
  • :wqa: Write all open windows/buffers and quit.
  • h, j, k, l: Move left, down, up, right.
  • w: Move to the start of the next word.
  • b: Move to the start of the previous word.
  • e: Move to the end of the current/next word.
  • 0 (zero): Move to the beginning of the line.
  • ^: Move to the first non-blank character of the line.
  • $: Move to the end of the line.
  • gg: Move to the very beginning of the file.
  • G: Move to the very end of the file.
  • <N>G: Move to line number <N> (e.g., 10G moves to line 10).
  • %: Move to the matching bracket/parenthesis/brace.
  • Ctrl+f: Scroll down one page (forward).
  • Ctrl+b: Scroll up one page (backward).
  • Ctrl+d: Scroll down half a page.
  • Ctrl+u: Scroll up half a page.
  • zz: Center the screen on the current line.

Editing (Normal Mode - Operators + Motions/Text Objects)

  • i: Enter Insert Mode before the cursor.
  • a: Enter Insert Mode after the cursor.
  • I: Enter Insert Mode at the beginning of the line.
  • A: Enter Insert Mode at the end of the line.
  • o: Open a new line below the current line and enter Insert Mode.
  • O: Open a new line above the current line and enter Insert Mode.
  • r<char>: Replace the character under the cursor with <char>.
  • R: Enter Replace Mode (overtype characters).
  • x: Delete the character under the cursor.
  • X: Delete the character before the cursor.
  • dd: Delete the current line.
  • <N>dd: Delete <N> lines starting from the current line (e.g., 5dd).
  • dw: Delete from the cursor to the end of the current word.
  • db: Delete from the cursor to the beginning of the current word.
  • d$: Delete from the cursor to the end of the line.
  • d0: Delete from the cursor to the beginning of the line (before the first character).
  • d^: Delete from the cursor to the first non-blank character of the line.
  • D: Delete from the cursor to the end of the line (d$).
  • yy: Yank (copy) the current line.
  • <N>yy: Yank <N> lines starting from the current line.
  • yw: Yank the current word.
  • y$: Yank to the end of the line.
  • Y: Yank the current line (yy).
  • p: Paste after the cursor/line.
  • P: Paste before the cursor/line.
  • c<motion>: Change text (delete and enter Insert Mode). Example: cw changes the current word.
  • C: Change to the end of the line (c$).
  • cc: Change the current line (c<motion> for the whole line).
  • u: Undo the last change.
  • Ctrl+r: Redo the last undone change.
  • . (period): Repeat the last change.
  • J: Join the current line with the next line.
  • ~: Swap case of the character under the cursor.
  • gU<motion>: Make text uppercase. Example: gUw makes the current word uppercase.
  • gu<motion>: Make text lowercase. Example: guw makes the current word lowercase.

Text Objects (Used with Operators)

  • i<text object>: Inner text object (e.g., diw deletes the inner word).
  • a<text object>: Around text object (includes surrounding characters, e.g., daw deletes a word and the space after it).

Common text objects:

  • w: Word
  • W: WORD (space-delimited)
  • s: Sentence
  • p: Paragraph
  • ( or ): Parenthesized text
  • { or }: Curly-braced text
  • [ or ]: Bracketed text
  • ": Double-quoted text
  • ': Single-quoted text
  • <: Angle-bracketed text

Visual Mode

  • v: Enter Visual Mode (character-wise).
  • V: Enter Visual Line Mode (line-wise).
  • Ctrl+v: Enter Visual Block Mode.
  • (In Visual Mode) y: Yank the selected text.
  • (In Visual Mode) d: Delete the selected text.
  • (In Visual Mode) c: Change the selected text.
  • (In Visual Mode) >: Indent the selected lines.
  • (In Visual Mode) <: Unindent the selected lines.
  • (In Visual Mode) u: Change selection to lowercase.
  • (In Visual Mode) U: Change selection to uppercase.

Search and Replace (Command-Line Mode)

  • /pattern: Search forward for pattern.
  • ?pattern: Search backward for pattern.
  • n: Move to the next match (in the direction of the last search).
  • N: Move to the previous match.
  • :%s/old/new/g: Replace all occurrences of old with new in the entire file.
    • %: Range (entire file).
    • s: Substitute command.
    • g: Global flag (replace all on a line, not just the first).
  • :%s/old/new/gc: Replace all occurrences, prompting for confirmation each time.
  • :<range>s/old/new/g: Replace within a specific range.
    • 10,20s/old/new/g: Replace on lines 10 through 20.
    • .,+5s/old/new/g: Replace on the current line and the next 5 lines.
    • '/start_pattern/','/end_pattern/s/old/new/g': Replace between lines matching start/end patterns.

Window Management

  • :split <filename>: Split the current window horizontally.
  • :vsplit <filename>: Split the current window vertically.
  • Ctrl+w followed by h, j, k, l: Move between split windows.
  • Ctrl+w followed by w or Ctrl+w Ctrl+w: Cycle through windows.
  • Ctrl+w followed by r: Rotate split windows clockwise.
  • Ctrl+w followed by R: Rotate split windows counter-clockwise.
  • Ctrl+w followed by _: Maximize the current window height.
  • Ctrl+w followed by |: Maximize the current window width.
  • Ctrl+w followed by c or :close: Close the current window.
  • Ctrl+w followed by o or :only: Close all windows except the current one.
  • :tabnew <filename>: Open a new tab.
  • gt: Go to the next tab.
  • gT: Go to the previous tab.
  • <N>gt: Go to tab number <N>.
  • :tabclose: Close the current tab.

Buffers

  • :ls or :buffers: List all open buffers.
  • :b <buffer_number>: Switch to buffer number <buffer_number>.
  • :b <buffer_name>: Switch to buffer with a name containing <buffer_name>.
  • :bn: Switch to the next buffer.
  • :bp: Switch to the previous buffer.
  • :bd: Delete (close) the current buffer.

Folding

  • zf: Create a fold.
  • zo: Open a fold.
  • zc: Close a fold.
  • zr: Open all folds recursively.
  • zm: Close all folds recursively.
  • zj: Move to the next fold boundary.
  • zk: Move to the previous fold boundary.

Macros

  • q<register>: Start recording a macro into <register> (e.g., qa records into register a).
  • (Perform actions)
  • q: Stop recording.
  • @<register>: Play back the macro in <register> (e.g., @a).
  • @@: Repeat the last executed macro.

Shell Commands

  • :!command: Execute <command> in the shell.
  • :r !command: Read the output of <command> into the current buffer.
  • Ctrl+v followed by j or k: Insert literal control characters.

Configuration (~/.vimrc)

  • set number: Display line numbers.
  • set relativenumber: Display relative line numbers.
  • set autoindent: Automatically indent new lines.
  • set smartindent: Smarter auto-indenting.
  • set tabstop=4: Set tab width to 4 spaces.
  • set shiftwidth=4: Set indentation width to 4 spaces.
  • set expandtab: Use spaces instead of tabs.
  • set hlsearch: Highlight search matches.
  • set incsearch: Show search matches incrementally as you type.
  • set syntax on: Enable syntax highlighting.
  • set mouse=a: Enable mouse support in all modes.
  • set wrap: Enable line wrapping (default).
  • set nowrap: Disable line wrapping.

Common Patterns

  • Copying a line and pasting it 5 times:

    5yyP
    

    (Yank 5 lines, then paste below the current line)

  • Deleting all lines except the first 10:

    11G
    dG
    

    (Go to line 11, delete from current line to the end)

  • Replacing all instances of "foo" with "bar" in the current file:

    :%s/foo/bar/g
    
  • Indenting multiple lines selected in Visual Mode: (Enter Visual Mode with V on the first line, move down to the last line, then press >)

  • Executing a command on lines containing a pattern:

    :/pattern/normal dd
    

    (Find lines matching pattern and delete them)

  • Saving and exiting from anywhere:

    :w!<CR>:q!<CR>
    

    (Write forcefully, then quit forcefully. <CR> is Enter/Return)

  • Finding a file and opening it in a split window:

    :sp **/somefile.txt
    

    (Uses globbing to find the file)

  • Quickly renaming a word under the cursor:

    ciwnewword<Esc>
    

    (Change Inner Word to newword, then exit Insert Mode)

Gotchas

  • Accidentally typing commands in Insert Mode: Always remember to press Esc to return to Normal Mode before executing commands.
  • Forgetting to save: Vim doesn’t auto-save. Use :w or :wq regularly.
  • Mode confusion: Especially for beginners, getting stuck in Insert Mode or Command-Line Mode and not knowing how to get back to Normal Mode is common. Esc is your friend.
  • Case sensitivity in searches: By default, searches are case-sensitive. Use \c in the search pattern (e.g., /pattern\c) for case-insensitive search, or set ignorecase and smartcase in your .vimrc.
  • Yanking vs. Deleting: Both d and y place text into registers (buffers). d deletes and yanks, y only yanks. p pastes from the "unnamed" register, which is usually the last deleted or yanked text.
  • Ctrl+v in terminal vs. Vim: In many terminals, Ctrl+v is used for special character insertion. In Vim’s Normal Mode, Ctrl+v enters Visual Block mode. If you want to insert a literal Ctrl+v in Vim’s Insert Mode, you might need to press Ctrl+q (depending on terminal configuration).
  • The . command: It repeats the last change. If your last change was typing "hello", . will type "hello". If your last change was deleting a line, . will delete a line. This can be powerful but sometimes surprising.