Mastering Vim: A Comprehensive Guide to Efficient Navigation and Editing
Learn Essential Commands, Advanced Techniques, and Customization Tips for Boosting Your Vim Productivity
Basic Navigation
h
- Move leftj
- Move downk
- Move upl
- Move rightw
- Move to the start of the next wordb
- Move to the start of the previous worde
- Move to the end of the word0
- Move to the beginning of the line$
- Move to the end of the linegg
- Go to the first line of the fileG
- 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 pageCtrl + b
- Move backward one page
Insert Mode
i
- Insert before the cursorI
- Insert at the beginning of the linea
- Insert after the cursorA
- Insert at the end of the lineo
- Open a new line below the current lineO
- Open a new line above the current lineEsc
- Exit insert mode
Editing
x
- Delete the character under the cursordd
- Delete (cut) the current lineyy
- Yank (copy) the current linep
- Paste after the cursorP
- Paste before the cursoru
- UndoCtrl + r
- Redo.
- Repeat the last commandr
- Replace the character under the cursorcw
- Change word (delete word and enter insert mode)cc
- Change line (delete line and enter insert mode)C
- Change from cursor to end of lines
- Delete character and enter insert modeS
- Delete line and enter insert mode
Search and Replace
/pattern
- Search forward forpattern
?pattern
- Search backward forpattern
n
- Go to the next matchN
- Go to the previous match:%s/old/new/g
- Replace all occurrences ofold
withnew
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 modeCtrl + v
- Enter visual block modey
- Yank (copy) selected textd
- Delete (cut) selected text>
- Indent selected text<
- Unindent selected text
File Operations
:w
- Save file:wq
or:x
orZZ
- 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 downCtrl + u
- Move half-page upH
- Move to the top of the screenM
- Move to the middle of the screenL
- 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 lowercasegUU
- Convert the current line to uppercaseg~
- Toggle case of the current character or selection>>
- Indent the current line<<
- Unindent the current lineJ
- Join the current line with the next linegq
- 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/rightCtrl + w + w
- Cycle through windowsCtrl + w + c
- Close the current windowCtrl + w + o
- Close all other windowsCtrl + w + =
- Equalize window sizesCtrl + w + _
- Maximize current window heightCtrl + w + |
- Maximize current window width:tabnew [file]
- Open a new tab (with[file]
if specified)gt
- Go to the next tabgT
- 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 foldzc
- Close a foldzR
- Open all foldszM
- Close all foldszd
- 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 withexit
):!%
- 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
Vim Official Documentation
https://vimhelp.org/
The official Vim documentation provides detailed information about all commands, features, and customization options.Vim Cheatsheet
https://vim.rtorr.com/
A concise and beginner-friendly cheatsheet that summarizes the most important Vim commands for quick reference.Vim Tutorial (OpenVim)
https://www.openvim.com/
An interactive Vim tutorial where you can practice commands directly in your browser.Vim Adventures
https://vim-adventures.com/
A fun, game-based way to learn Vim commands and navigation techniques in an interactive setting.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.