Mastering Vim: A Comprehensive Guide to Efficient Navigation and Editing

Learn Essential Commands, Advanced Techniques, and Customization Tips for Boosting Your Vim Productivity

Mastering Vim: A Comprehensive Guide to Efficient Navigation and Editing

Basic Navigation

  • h - Move left

  • j - Move down

  • k - Move up

  • l - Move 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 word

  • 0 - Move to the beginning of the line

  • $ - Move to the end of the line

  • gg - Go to the first line of the file

  • G - Go to the last line of the file

  • :[n] - Go to line number [n] (e.g., :10 goes to line 10)

  • Ctrl + f - Move forward one page

  • Ctrl + b - Move backward one page

Insert Mode

  • i - Insert before the cursor

  • I - Insert at the beginning of the line

  • a - Insert after the cursor

  • A - Insert at the end of the line

  • o - Open a new line below the current line

  • O - Open a new line above the current line

  • Esc - Exit insert mode

Editing

  • x - Delete the character under the cursor

  • dd - Delete (cut) the current line

  • yy - Yank (copy) the current line

  • p - Paste after the cursor

  • P - Paste before the cursor

  • u - Undo

  • Ctrl + r - Redo

  • . - Repeat the last command

  • r - Replace the character under the cursor

  • cw - Change word (delete word and enter insert mode)

  • cc - Change line (delete line and enter insert mode)

  • C - Change from cursor to end of line

  • s - Delete character and enter insert mode

  • S - Delete line and enter insert mode

Search and Replace

  • /pattern - Search forward for pattern

  • ?pattern - Search backward for pattern

  • n - Go to the next match

  • N - Go to the previous match

  • :%s/old/new/g - Replace all occurrences of old with new in the file

  • :%s/old/new/gc - Replace with confirmation

  • :s/old/new/g - Replace in the current line

Visual Mode

  • v - Enter visual mode (character-wise)

  • V - Enter visual line mode

  • Ctrl + v - Enter visual block mode

  • y - Yank (copy) selected text

  • d - Delete (cut) selected text

  • > - Indent selected text

  • < - Unindent selected text

File Operations

  • :w - Save file

  • :wq or :x or ZZ - Save and quit

  • :q - Quit (fails if there are unsaved changes)

  • :q! - Quit without saving

  • :e [file] - Open another file

  • :w [file] - Save as [file]

  • :r [file] - Insert contents of [file] below the cursor

Miscellaneous

  • :set number - Show line numbers

  • :set nonumber - Hide line numbers

  • :set paste - Enable paste mode (prevents auto-indentation)

  • :set nopaste - Disable paste mode

  • :help [command] - Open help for [command]

  • Ctrl + g - Show file status and current line

Advanced Navigation

  • Ctrl + d - Move half-page down

  • Ctrl + u - Move half-page up

  • H - Move to the top of the screen

  • M - Move to the middle of the screen

  • L - Move to the bottom of the screen

  • } - Move to the next paragraph or block

  • { - Move to the previous paragraph or block

  • % - Jump to the matching bracket (), {}, or []

  • * - Search for the word under the cursor (forward)

  • # - Search for the word under the cursor (backward)

  • gd - Go to the definition of the word under the cursor (local)

  • gD - Go to the definition of the word under the cursor (global)

Advanced Editing

  • di[ - Delete inside [] (works with other delimiters like (), {}, "", etc.)

  • da[ - Delete around [] (includes the delimiters)

  • ci[ - Change inside [] (delete and enter insert mode)

  • ca[ - Change around [] (delete including delimiters and enter insert mode)

  • dt[x] - Delete until character [x]

  • ct[x] - Change until character [x]

  • guu - Convert the current line to lowercase

  • gUU - Convert the current line to uppercase

  • g~ - Toggle case of the current character or selection

  • >> - Indent the current line

  • << - Unindent the current line

  • J - Join the current line with the next line

  • gq - Format selected text or paragraph

  • :m [n] - Move the current line to after line [n]

  • :t [n] - Copy the current line to after line [n]

Macros

  • q[a-z] - Start recording a macro into register [a-z]

  • q - Stop recording the macro

  • @[a-z] - Execute the macro stored in register [a-z]

  • @@ - Repeat the last executed macro

Windows and Tabs

  • :sp [file] - Split window horizontally (open [file] if specified)

  • :vsp [file] - Split window vertically (open [file] if specified)

  • Ctrl + w + h/j/k/l - Move to the window left/down/up/right

  • Ctrl + w + w - Cycle through windows

  • Ctrl + w + c - Close the current window

  • Ctrl + w + o - Close all other windows

  • Ctrl + w + = - Equalize window sizes

  • Ctrl + w + _ - Maximize current window height

  • Ctrl + w + | - Maximize current window width

  • :tabnew [file] - Open a new tab (with [file] if specified)

  • gt - Go to the next tab

  • gT - Go to the previous tab

  • :tabclose - Close the current tab

  • :tabonly - Close all other tabs

Registers

  • "[a-z]y - Yank (copy) into register [a-z]

  • "[a-z]p - Paste from register [a-z]

  • :reg - View the contents of all registers

  • "+y - Yank to the system clipboard

  • "+p - Paste from the system clipboard

Search and Replace with Regular Expressions

  • /\vpattern - Use "very magic" mode for regex (simplifies escaping)

  • /pattern\c - Case-insensitive search

  • /pattern\C - Case-sensitive search

  • :%s/\vold/new/g - Use regex for search and replace

  • :%s/\<word\>/new/g - Replace whole words only

  • :%s/old/new/gI - Replace with case-insensitive search

Folding

  • zf - Create a fold (visual mode or motion-based, e.g., zfap for a paragraph)

  • zo - Open a fold

  • zc - Close a fold

  • zR - Open all folds

  • zM - Close all folds

  • zd - Delete a fold

  • :set foldmethod=indent - Fold based on indentation

  • :set foldmethod=syntax - Fold based on syntax

Command-Line Tricks

  • :!command - Run a shell command (e.g., :!ls)

  • :r !command - Insert the output of a shell command into the file

  • :sh - Open a shell (return to Vim with exit)

  • :!% - Run the current file as a script (if executable)

  • :read [file] - Insert the contents of [file] below the cursor

  • :write !sudo tee % - Save a file with sudo permissions

Plugins and Customization

  • :PluginInstall - Install plugins (if using a plugin manager like vim-plug)

  • :PluginUpdate - Update installed plugins

  • :PluginClean - Remove unused plugins

  • :set [option] - Enable a Vim option (e.g., :set number)

  • :set no[option] - Disable a Vim option (e.g., :set nonumber)

  • :colorscheme [name] - Change the color scheme

  • :syntax on - Enable syntax highlighting

  • :syntax off - Disable syntax highlighting

Marks

  • m[a-z] - Set a mark at the current position (e.g., ma)

  • '[a-z] - Jump to the line of mark [a-z]

  • ``[a-z]- Jump to the exact position of mark[a-z]`

  • :marks - List all marks

Buffers

  • :e [file] - Open a file in a new buffer

  • :bnext or :bn - Go to the next buffer

  • :bprev or :bp - Go to the previous buffer

  • :bd - Close the current buffer

  • :ls - List all open buffers

  • :b [n] - Switch to buffer [n]

  • :b [name] - Switch to buffer by name (partial match)

Reference

  1. Vim Official Documentation
    https://vimhelp.org/
    The official Vim documentation provides detailed information about all commands, features, and customization options.

  2. Vim Cheatsheet
    https://vim.rtorr.com/
    A concise and beginner-friendly cheatsheet that summarizes the most important Vim commands for quick reference.

  3. Vim Tutorial (OpenVim)
    https://www.openvim.com/
    An interactive Vim tutorial where you can practice commands directly in your browser.

  4. Vim Adventures
    https://vim-adventures.com/
    A fun, game-based way to learn Vim commands and navigation techniques in an interactive setting.

  5. Vim Tips Wiki
    http://vim.wikia.com/wiki/Vim_Tips_Wiki
    A community-driven collection of Vim tips, tricks, and guides for both beginners and advanced users.