Vim Cheat Sheet

Vim is a highly configurable text editor that is widely used for code editing and other text manipulation tasks. Below is a basic cheat sheet with commonly used Vim commands:

Modes

  • Normal Mode (default):
    • Press Esc to enter Normal Mode.
  • Insert Mode:
    • Press i to enter Insert Mode (start typing text).
    • Press I to insert at the beginning of the line.
    • Press a to append after the cursor.
    • Press A to append at the end of the line.
    • Press o to open a new line below the current line.
    • Press O to open a new line above the current line.
  • Visual Mode:
    • Press v to enter Visual Mode.
    • Press V for line-wise Visual Mode.
    • Press Ctrl + v for block-wise Visual Mode.

Basic Navigation

  • Move Cursor:
    • h, j, k, l: Left, down, up, right.
    • w, b: Move by word forward, backward.
    • ^, $: Move to the beginning, end of the line.
    • gg, G: Move to the beginning, end of the file.
  • Scrolling:
    • Ctrl + u, Ctrl + d: Scroll half page up, down.
    • Ctrl + b, Ctrl + f: Scroll full page up, down.

Editing

  • Copy, Cut, Paste:
    • yy: Copy (yank) line.
    • y$: Copy to the end of the line.
    • dd: Cut (delete) line.
    • D: Cut to the end of the line.
    • p: Paste after the cursor.
    • P: Paste before the cursor.
  • Undo, Redo:
    • u: Undo.
    • Ctrl + r: Redo.
  • Find and Replace:
    • :/pattern: Search for a pattern.
    • n: Move to the next match.
    • N: Move to the previous match.
    • :%s/old_text/new_text/g: Replace all occurrences.

Marking and Navigation

  • Marking:
    • m{letter}: Set a mark at the cursor position.
    • ` {letter}: Jump to the mark.

Saving and Exiting

  • Save and Quit:
    • :w: Save.
    • :q: Quit.
    • :wq or ZZ: Save and quit.
  • Force Quit (Discard Changes):
    • :q!: Quit without saving.

Splitting Windows

  • Horizontal Split:
    • :split or :sp: Split horizontally.
  • Vertical Split:
    • :vsplit or :vsp: Split vertically.
  • Switching Between Windows:
    • Ctrl + w + Arrow keys: Switch between windows.

File Explorer

  • Open File Explorer:
    • :e .: Open file explorer.

Miscellaneous

  • Repeat Last Command:
    • .: Repeat last command.
  • Indentation:
    • >>: Indent current line.
    • <<: Unindent current line.
  • Replace Mode:
    • R: Enter Replace Mode.

Help and Miscellaneous

  • Help:
    • :help: Open help.

Exiting Vim (for beginners)

  • Save and Quit:
    • Press Esc to enter Normal Mode.
    • Type :wq and press Enter.
  • Quit without Saving:
    • Press Esc to enter Normal Mode.
    • Type :q! and press Enter.

Note

  • Vim has a steep learning curve, and these are just basic commands.
  • To explore more commands and features, refer to Vim’s extensive documentation and tutorials.
  • Vim’s power lies in its efficiency and extensive set of features, so take time to learn and customize it according to your preferences.