Mastering Vim: A Cheatsheet Guide for Fast & Efficient Text Editing

Vim is a powerful, keyboard-driven text editor that excels in speed and efficiency. This cheatsheet covers essential commands for navigation, editing, and file management—perfect for beginners or anyone looking to boost their productivity in Vim.

Mastering Vim: A Cheatsheet Guide for Fast & Efficient Text Editing
Created with ChatGPT

Vim is a powerful, modal text editor that offers unmatched speed and control — once you overcome the steep learning curve. Whether you're a beginner or brushing up, this cheatsheet-style article will help you quickly get up to speed and boost your productivity.

🧠 Understanding Vim Modes

Vim operates in modes — each with a specific purpose:

  • Normal mode: for navigation and editing (press Esc to enter)
  • Insert mode: for typing text (i, a, o, etc.)
  • Visual mode: for selecting text (v, V, Ctrl+v)
  • Command-line mode: for commands like saving and quitting (: from Normal mode)

🧭 Movement

Move the cursor:

  • h – left
  • j – down
  • k – up
  • l – right

Word and line navigation:

  • w – next word
  • b – previous word
  • e – end of word
  • 0 – beginning of line
  • ^ – first non-blank character of line
  • $ – end of line

File navigation:

  • gg – start of file
  • G – end of file
  • :n – go to line n (e.g. :3 moves to line 3)
  • % – jump between matching () {} []

Scrolling:

  • Ctrl-d – scroll down half-page
  • Ctrl-u – scroll up half-page
  • Ctrl-f – scroll forward one page
  • Ctrl-b – scroll back one page

✍️ Inserting Text

  • i – insert before cursor
  • I – insert at beginning of line
  • a – append after cursor
  • A – append at end of line
  • o – open new line below
  • O – open new line above

🔧 Editing

  • x – delete character under cursor
  • X – delete character before cursor
  • r<char> – replace character
  • R – enter replace mode
  • J – join current line with the next
  • u – undo
  • Ctrl-r – redo
  • . – repeat last command

📋 Copy, Paste, and Delete

  • yy – yank (copy) current line
  • y<motion> – yank based on motion (e.g. yw, y$)
  • p – paste after cursor
  • P – paste before cursor
  • dd – delete current line
  • d<motion> – delete based on motion (e.g. dw, d$)

Working with registers:

  • "ay – yank into register a
  • "ap – paste from register a

🔍 Search and Replace

Searching:

  • /pattern – search forward
  • ?pattern – search backward
  • n – repeat search forward
  • N – repeat search backward

Replacing:

  • :s/old/new/ – replace first match on current line
  • :s/old/new/g – replace all matches on current line
  • :%s/old/new/g – replace all in file
  • :%s/old/new/gc – replace all with confirmation

📐 Visual Mode (Selection)

  • v – start visual selection (character mode)
  • V – linewise selection
  • Ctrl+v – blockwise selection
  • d – delete selection
  • y – yank selection
  • > – indent selection
  • < – un-indent selection
  • ~ – toggle case of selection

💾 File and Buffer Commands

File operations:

  • :e filename – open file
  • :w – save
  • :w filename – save as
  • :q – quit
  • :q! – quit without saving
  • :wq or ZZ – save and quit
  • :x – save if needed and quit

Buffers (multiple open files):

  • :ls – list open buffers
  • :bnext or :bn – next buffer
  • :bprev or :bp – previous buffer
  • :bd – delete (close) buffer
  • :b <number> – switch to buffer

🔁 Macros

  • q<register> – start recording macro to register
  • <commands> – perform your actions
  • q – stop recording
  • @<register> – play macro
  • @@ – repeat last macro

⚙️ Useful Settings

  • :set number – show line numbers
  • :set relativenumber – show relative line numbers
  • :set ignorecase – ignore case in search
  • :set smartcase – ignore case unless uppercase used
  • :set hlsearch – highlight search matches
  • :set incsearch – incremental search
  • :syntax on – turn on syntax highlighting
  • :noh – clear search highlight

📌 Essential Commands Recap

  • Save: :w
  • Quit: :q
  • Save and Quit: :wq
  • Quit without saving: :q!
  • Search: /pattern
  • Replace all: :%s/old/new/g
  • Undo: u
  • Redo: Ctrl-r
  • Open file: :e filename

✅ Tips

  • Start slow. Learn hjkl, i, :wq, and /search first.
  • Practice motions with commands (d, y, c) to operate efficiently.
  • Use . to repeat last change — it’s a huge time-saver.
  • Explore :help for anything (:help :wq, :help visual).