Vim Quick Reference
Efficient text editing in the terminal – once you learn it, you'll never go back.
Opening and Exiting Vim
# Open file vim filename nvim filename # Open multiple files vim file1 file2 file3 # Open at specific line vim +10 filename vim +/search_pattern filename # Exit :q # quit (fails if unsaved) :q! # force quit (discard changes) :wq # save and quit :x # save and quit (same as :wq) ZZ # save and quit (normal mode) ZQ # force quit (normal mode)
Modes
| Mode | Purpose | How to Enter |
|---|---|---|
| Normal | Navigation, commands | Esc, Ctrl+[ |
| Insert | Typing text | i, a, o, etc. |
| Visual | Selecting text | v, V, Ctrl+v |
| Command | Ex commands (:) | : from normal mode |
| Replace | Overwrite text | R |
| Terminal | Run shell commands (Neovim) | :term |
Navigation (Normal Mode)
Basic Movement
h # left j # down k # up l # right w # forward by word W # forward by WORD (punctuation ignored) b # backward by word B # backward by WORD e # end of word E # end of WORD 0 # beginning of line ^ # first non‑blank character $ # end of line gg # beginning of file G # end of file :5 # go to line 5 5G # go to line 5 Ctrl+u # half‑page up Ctrl+d # half‑page down Ctrl+f # full page down Ctrl+b # full page up % # jump to matching bracket/paren H # top of screen M # middle of screen L # bottom of screen
Search
/pattern # search forward ?pattern # search backward n # next match N # previous match * # search forward for word under cursor # # search backward for word under cursor :noh # clear search highlighting
Editing (Insert Mode)
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 s # delete character and insert S # delete line and insert c{motion} # change (delete + insert) C # change to end of line cc # change entire line r # replace single character R # enter replace mode Ctrl+n # autocomplete (insert mode) Ctrl+p # autocomplete backwards
Visual Mode (Selecting Text)
v # character‑wise visual V # line‑wise visual Ctrl+v # block‑wise visual gv # reselect previous visual selection # After selection: d # delete selected y # yank (copy) selected p # paste over selection > # indent right < # indent left = # auto‑format u # lowercase U # uppercase ~ # toggle case
Cut, Copy, Paste (Registers)
# Delete (cut) – also stored in register d # delete (cut) dd # delete line D # delete to end of line dw # delete word d$ # delete to end of line d0 # delete to beginning of line dgg # delete to beginning of file dG # delete to end of file # Yank (copy) y # yank (copy) yy # yank line Y # yank to end of line yw # yank word y$ # yank to end of line # Put (paste) p # paste after cursor (or below line) P # paste before cursor (or above line) ]p # paste with auto‑indent # Named registers (a‑z) "ayy # yank line into register a "ap # paste from register a # System clipboard (requires +clipboard) "+y # copy to system clipboard "+p # paste from system clipboard "*y # copy to primary selection (X11)
Undo, Redo, Repeat
u # undo Ctrl+r # redo . # repeat last change 5. # repeat 5 times
Search and Replace
# Simple replace in current line :s/old/new/ # replace first occurrence :s/old/new/g # replace all occurrences # Replace in file :%s/old/new/g # replace all occurrences globally :%s/old/new/gc # with confirmation # Replace in specific lines :10,20s/old/new/g # lines 10‑20 # With regex :%s/\/new/g # whole word match :%s/foo/bar/g # plain text :%s/foo/bar/gi # case‑insensitive # Use register in replacement :%s//new/g # repeat last search pattern # Visual selection replace :'<,'>s/old/new/g # after visual selection
Macros
# Record macro into register a qa # start recording ... # perform actions q # stop recording # Play macro @a # play once 5@a # play 5 times @@ # repeat last macro # View macro contents :reg a # show register a
Multiple Files / Windows
Buffers
:ls # list buffers :b 2 # go to buffer 2 :bnext # next buffer :bprev # previous buffer :bd # delete buffer :bp # previous buffer
Splits
:split # horizontal split (or :sp) :vsplit # vertical split (or :vs) Ctrl+w h # move to left split Ctrl+w l # move to right split Ctrl+w j # move to lower split Ctrl+w k # move to upper split Ctrl+w w # cycle splits Ctrl+w q # close split Ctrl+w = # equalize splits Ctrl+w _ # maximise height Ctrl+w | # maximise width :only # close all other splits
Tabs
:tabnew # new tab :tabnext # next tab :tabprev # previous tab gt # next tab gT # previous tab :tabclose # close tab
Indentation and Formatting
# Indent >> # indent right (normal mode) << # indent left (normal mode) > # indent visual selection < # outdent visual selection = # auto‑format visual selection gg=G # auto‑format entire file # Set indentation :set tabstop=4 :set shiftwidth=4 :set expandtab # use spaces :set noexpandtab # use tabs :retab # convert tabs/spaces according to settings
Command‑line Commands (Ex Mode)
# Save :w # save :w! # force save :w newfile # save as # Quit :q # quit :q! # force quit :wq # save and quit # Open :e file # edit file :e! # reload file (discard changes) # Shell :!command # run shell command :shell # open shell (exit to return) # Set options :set number # show line numbers :set nonumber # hide line numbers :set relativenumber # relative line numbers :set hlsearch # highlight search matches :set nohlsearch # disable highlight :set ignorecase # case‑insensitive search :set smartcase # case‑sensitive if uppercase used :set incsearch # incremental search # Fold :set foldmethod=indent :set foldmethod=marker za # toggle fold zA # toggle fold recursively zo # open fold zc # close fold zR # open all folds zM # close all folds
Configuration (~/.vimrc or ~/.config/nvim/init.vim)
" Basic settings set number set relativenumber set tabstop=4 set shiftwidth=4 set expandtab set hlsearch set incsearch set ignorecase set smartcase set autoindent set smartindent set wrap set mouse=a " Syntax highlighting syntax enable syntax on " Filetype detection filetype on filetype plugin on filetype indent on " Leader key let mapleader = ' ' " Mappings nnoremapw :w nnoremap q :q nnoremap x :x " Search and replace word under cursor nnoremap r :%s/\< \>//g " Remove trailing whitespace autocmd BufWritePre * %s/\s\+$//e
Useful Plugins
| Plugin | Purpose |
|---|---|
| vim-plug | Plugin manager |
| fzf.vim | Fuzzy finder |
| nerdtree | File explorer |
| coc.nvim | Language Server Protocol (LSP) client |
| telescope.nvim | Fuzzy finder (Neovim) |
| vim-airline | Status bar |
| vim-fugitive | Git integration |
| tcomment_vim | Comment/uncomment |
| surround.vim | Add/change surroundings (brackets, quotes) |
Common Text Objects
# Text objects (can be used with d, y, c, v, etc.) aw # a word iw # inner word as # a sentence is # inner sentence ap # a paragraph ip # inner paragraph a" # a quoted string (including quotes) i" # inner quoted string a' # a single-quoted string i' # inner single-quoted string a( # a parenthesized block i( # inner parentheses a[ # a bracket block i[ # inner brackets a{ # a brace block i{ # inner braces a< # a tag block (HTML/XML) i< # inner tag block # Examples ciw # change inner word di" # delete inside quotes ya( # yank including parentheses vi{ # select inside braces
Best Practices
- Learn the navigation keys – avoid arrow keys.
- Use
.to repeat – combine with motions for efficiency. - Use macros – for repetitive tasks.
- Use text objects – instead of counting characters.
- Use relative line numbers – for easy jumping (
5j,3k). - Learn
:help– built‑in documentation is excellent. - Start with a minimal config – add plugins gradually.
- Use
vim-plug– for plugin management. - Use
vim-tmux-navigator– if you use tmux. - Commit your
.vimrc– to version control. - Practice – do Vim adventures or use Vim for all text editing.
Quick Troubleshooting
- Can't type? – you're in normal mode. Press
i. - Can't save? – check file permissions or use
:w!. - Search highlights stuck? –
:noh. - Accidentally closed Vim? –
:qor:q!. - Line numbers not showing? –
:set number. - Syntax highlighting off? –
:syntax on.
📌 Quick Reference
Modes: Normal (Esc) – Insert (i) – Visual (v) – Command (:)
Navigation: h,j,k,l | w,b,e | gg,G | /search, n/N
Editing: i,a,o | d,y,p | u, Ctrl+r | .
Search/Replace: :%s/old/new/g
Registers: "a yank, "ap paste, "+ for clipboard
Macros: qa record, @a play
Windows: :split, :vsplit, Ctrl+w navigation
Best tip: Learn one new command a day.
Navigation: h,j,k,l | w,b,e | gg,G | /search, n/N
Editing: i,a,o | d,y,p | u, Ctrl+r | .
Search/Replace: :%s/old/new/g
Registers: "a yank, "ap paste, "+ for clipboard
Macros: qa record, @a play
Windows: :split, :vsplit, Ctrl+w navigation
Best tip: Learn one new command a day.