Vim Cheatsheet
Aug 1, 2022
5 min read
Vim is a fantastic code editor and is a popular choice among hardcore developers. It’s my favourite choice when it comes to making quick code changes in terminal. Vim has a huge learning curve with its long list commands to memorise. Here is a cheatsheet with my most used vim commands in my development process.
" SAVE & EXITS
<Esc> :qa " close all files
<Esc> :qa! " close all files, ignore changes
<Esc> :w " write changes to file
<Esc> :wq " write changes to file and quit
<Esc> :x
<Esc> :q " close file
<Esc> :q! " close file, ignore if any changes
<Esc> ZZ " save and quit from vim
" NAVIGATION - PAGE SCROLL
" C -> Ctrl Key
<Esc> h " left arrow key
<Esc> j " down arrow key
<Esc> k " up arrow key
<Esc> l " right arrow key
<Esc> <C-u> " half page up
<Esc> <C-d> " half page down
<Esc> <C-b> " page up
<Esc> <C-f> " page down
" NAVIGATION - WORD NAVIGATION
<Esc> w " move cursor word by word in forward direction
<Esc> b " move cursor word by word in backward direction
<Esc> e " move cursor word by word in forward direction + cursor will be at the end of the word
<Esc> ge " move cursor word by word in backward direction + cursor will be at the end of the word
" NAVIGATION - LINE NAVIGATION
<Esc> 0 " move cursor to the start of the line
<Esc> ^ " move cursor to the start of the line
<Esc> $ " move cursor to the end of the line
" NAVIGATION - CHARACTER NAVIGATION
<Esc> fc " go forward to character c
<Esc> Fc " go backward to character c
" NAVIGATION - DOCUMENT NAVIGATION
<Esc> gg " go to first line
<Esc> G " go to last line
<Esc> nG " go to line n without entering command
<Esc> :10 " go to line 10
" NAVIGATION - WINDOW NAVIGATION
<Esc> zz " center align the page with respect to the position of the cursor
<Esc> zt " top align the page with respect to the position of the cursor
<Esc> zb " bottom align the page with respect to the position of the cursor
<Esc> H " move to top of the screen
<Esc> M " move to middle of the screen
<Esc> L " move to bottom of the screen
" SEARCH
<Esc> /key " enter keyword to search in the current page
<Esc> n " next matching search pattern
<Esc> N " previous matching search pattern
<Esc> * " next whole word under cursor
<Esc> # " previous whole word under cursor
" TAB PAGES
<Esc> :tabedit " edit a file in new tab
<Esc> :tabfind " open file in new tab if it exists in the current directory recursively
<Esc> :tabclose " close current tab
<Esc> :tabs " list all tabs
<Esc> :tabfirst " go to first tab
<Esc> :tablast " go to last tab
<Esc> :tabn " go to next tab
<Esc> :tabp " go to previous tab
" EXIT INSERT MODE
<Esc> <C-c> " exit insert mode and abort current command
<Esc> " exit insert mode
" VISUAL MODE
<Esc> v " enter visual mode
<Esc> V " enter visual line mode
<Esc> d " delete selection
<Esc> x
<Esc> S " replace selection
<Esc> y " yank selection
" EDITING
<Esc> a " append
<Esc> A " append from end of the line
<Esc> i " insert mode
<Esc> o " next line
<Esc> O " previous line
<Esc> s " delete character and insert
<Esc> S " delete line and insert
<Esc> C " delete until end of the line and insert
<Esc> r " replace one character
<Esc> R " enter replace mode
<Esc> u " undo changes
<Esc> <C-r> " redo changes
<Esc> c " change (delete then insert)
<Esc> >> " indent right
<Esc> << " indent left
<Esc> == " auto indent
<Esc> g~ " swap case
<Esc> gU " upper case
<Esc> gu " lower case
<Esc> dw " delete word in forward direction
<Esc> db " delete word in backward direction
<Esc> 2dd " delete 2 lines
" COPY and PASTE
<Esc> x " delete character
<Esc> dd " cut line
<Esc> yy " copy line (yank)
<Esc> p " paste after
<Esc> P " paste before
<Esc> vip " select paragraph
<Esc> vipipip " increment paragraph selection
<Esc> yip " yank paragraph
<Esc> yap " yank paragraph including new line
<Esc> dip " delete paragraph
<Esc> dap " delete paragraph including new line
" SPELL CHECK
<Esc> ]s " move to next misspelled word
<Esc> [s " move to previous misspelled word
<Esc> z= " suggest spelling for the word
<Esc> zg " add word to spell list
<Esc> zw " mark word as bad / misspelling
" BULK EDIT
<Esc> <C-v> " Ctrl+v to start visual selection and then use arrow keys for multi-line selection
<Shift> I " insert your text and hit escape, wait for other lines to update
" FIND AND REPLACE TEXT
<Esc> :%s/a/A/g " find and replace a with A in the entire file
<Esc> :s/a/A/ " find and replace a with A for only the first occurrences
<Esc> :s/a/A/g " find and replace a with A for all occurrences in the current line
<Esc> :%s/a/A/gc " find and replace a with A in the entire file after confirming for each substitution