These are the Vim key bindings and commands that I use the most, collected in one place so I don’t have to look them up again. At the end there are also a few plugins (Vim Vinegar, NERDTree, CtrlP, Ack, Greplace, Surround), FZF and tmux that I use together with Vim.
Generate Ctags
This is the ctags command I use to generate the tags file for a JavaScript and PHP project:
ctags -R --languages=JavaScript,PHP \ --JavaScript-kinds=fcmpCv \ --PHP-kinds=cdfintv \ --exclude=.git \ --exclude=node_modules \ --exclude=public \ --exclude=*.min.js \Modes
i: insert modev: visual mode:: commandesc: back to normal mode!ls: Execute thelscommand:help foo: Find helpCTRL + ]: Follow the hyperlink
File Editing
:w: write/save:w foo.txt: save file:wa: Save all buffers:e foo.txt: edit file:e .: Browse file to edit:Ex: Explore the directory where currently opened file is located:q: quit:wq: write and quit:so ~/.vimrc: Source thevimrcfile:so %: Source the current file
Movements
h: leftl: rightj: downk: upw: Move forward 1 wordW: Move forward 1 word, next non-whitespace charactersb: Move backward 1 wordB: Move backward 1 word (previous non-white space characters)e: Go to end of word0: Move to the beginning line^: Move the first non-blank character of the line$: Move to the end of line): Jump forward 1 sentence(: Jump backward 1 sentence}: Jump forward 1 paragraph{: Jump backward 1 paragraphH: Jump to the top of the screenM: Jump to the middle of the screenL: Jump to the bottom of the screengg: Jump to the top of the fileG: Jump to the end of the file15G: Jump to the line 15CTRL + f: Scroll page forwardCTRL + b: Scroll page backwardfx: Find the nextxcharacter on the current lineFx: Find the previousxcharacter on the current linetx: Find thexcharacter on the current line but place the cursor before the character
Jump through the changes:
:changes: List of changesg;: Cycle through the changes cursor position backwardg,: Cycle through the changes cursor position forward
Marks
:marks: Show all marksmx: Set local mark x at the current cursor position (lowercase mark will only available on current buffer)mX: Set global mark X at the current cursor position (uppercase mark will available across all buffers)'x: Jump to the beginning of the line of mark x`x: Jump the cursor of the mark xd'x: Delete until mark x'0: Go to mark 0, the last file we’re working on (live across sessions, even when vim closed)
Editing
V: Visual mode select lined: deleteu: undo. MODAL EDITING! 1 step of undo is equals to editing then<esc>back to normal mode. Always back to normal mode to persist the undo steps as many as we want!CTRL + r: redogv: Select previous selection again- In select mode
CTRL +d: select down 1 screen
Entering the insert mode:
aGo to the insert mode after the current cursor positionA: Go to the end of the line and enter insert mode (equals to$a).sDelete character under cursor and go to insert moder: Replace single character under cursor and back to normal modex: Delete single character under cursor and back to normal modeC: Change until the end of line (equalsd$a)D: Delete until the end of lineI: Go to the first non-whitespace character on the line and enter the insert modeS: Delete the entire line and enter the insert mode on the previous non-whitespace character (equals to^C)ci(: Change inside the bracket(. . .)ci{: Change inside the curly braces{. . . }ca(: Change inside the bracket, including the bracket itself( . . . )vi(: Select inside the bracket(. . .)di(: Delete inside the bracketcit: Change inside the tag<h1>. . . </h1>
Search and replace:
/<CTRL+r>": Search and paste the last yanked item- Select the text and
:s/foo/bar: Replacefoowithbarin the selected text :%s/foo/bar/g: Replace all matchingfoowithbar:%s/Foo/bar/gi: Replace all matchingfoowithbar, case insensitive search:%s/foo/bar/gc: Replace all matchingfoowithbarwith confirmation
File type:
:set filetype?: Check the current file type:set filetype=html: Set the file type
Registers
"xyy: Yank the current line and store it inxregister"xp: Paste thexregister (normal mode)CTRL+R x: Paste thexregister (insert mode)"*p: Paste the system clipboard (normal mode)CTRL+R *: Paste from system clipboard (insert mode)
Autocomplete
CTRL-X Mode
set complete=.,w,b,u,t,i: Set the completion source.: Scan the current bufferw: Scan the buffers from other windows (splits)b: Scan the buffers from the buffer list (the active ona)u: Scan the unloaded buffers from the buffer listt: Scan from the tags filei: Scan the included files (files listed using#include,require, etc)
CTRL+X CTRL+L: Search backward for a LINE that starts with the same charactersCTRL+X CTRL+N: Search forward for a WORD that start with the keyword before the cursor (only in current buffer)CTRL+X CTRL+P: Search backward for a WORD that start with the keyword before the cursor (only in current buffer)CTRL+X CTRL+I: Search current file and any included filesCTRL+X CTRL+]: Search using the tags fileCTRL+X CTRL+F: Search for file names that starts with the same charactersCTRL+X CTRL+O: Automatically guest what kind of item before the cursor (search current buffer, tags file and builtin VIM omni completion)
When popup open, we can use this keys:
CTRL+N: Next matchCTRL+P: Previous matchCTRL+E: Go back to the originally typed textCTRL+Y: Accept the currently selected item on popup menu
Other completion keys:
CTRL+N: Find the next match for words that start with the keyword before the cursorCTRL+P: Find the previous match for words that start with the keyword before the cursorCTRL+w z: Close the preview window
Tabs
:tabe foo.txt: Edit file in a new tab:tabnew: Open a new tab:tabc: Close the tab:tabonly: Close all tabs except the active onegt: Cycle through tab forwardgT: Cycle through tab backward2gt: Go to the second tab3gt: Go to the third tabtabmove: Move tab to the endtabmove 0: Move tab to the beginningtabmove 1: Move tab to the second one:pwd: Check current working directory:cd /foo/bar: Change the current working directory of a tab
Buffers and Splits
:ls: List buffers:bp: Open previous buffer:bn: Open next buffer:bd: Destroy the bufferCTRL + w + o: Make the current buffer full screenCTRL + ^: Toggle between two alternate buffers:bufdo bd!: Close all buffers:set hidden: Make hidden buffer without annoying save warning:e!: Restore the original file:tab sb 2: Open buffer number 2 in new tab
Splits:
:sp: Create horizontal split:vsp: Create vertical split:only: Close other windows and keep the active oneCTRL + wh: Switch to the leftCTRL + wl: Switch to the right splitCTRL + wj: Switch to the top splitCTRL + wk: Switch to the bottom splitCTRL + ww: Toggle between splitCTRL + w + |: Maximize the current window verticallyCTRL + w + _: Maximize the current window horizontallyCTRL + w + =: Make the splits equal in sizeCTRL + w + T: Move current window to a new tab
Vim Vinegar
-: Open the browser or move up a directory%: Create a new filed: Create a new directoryD: Delete a file/directoryR: Rename file/directory
NERDTree
t: Open in a new tabi: Open in new horizontal splits: Open in new vertical splito: Open and close a nodeO: Open node recursivelyx: Close a nodeX: Close the node recursivelyp: Go to parent nodeP: Go to the root node
CtrlP
CTRL + d: Search by filename onlyF5: Refresh the file list
Ctags
!ctags -R: Generate tags recursively!ctags -a foo.php: Append the file to tags file:tag term: Search for a term:tn: Go to the next matching term:tp: Go to the previous matching term:ts: Open the list of matching termsCTRL + ]: Go to the term under the cursor
Find and Replace
grep -R 'John Doe' ./: Manual grep from command linecopen: Open the previously matched grep
Ack Vim
:Ack 'term': Find the term:Ack 'term' foo/bar: Find the term infoo/barO: Open the file and close the quickview windowt: Open the file in a new tabT: Open the file in a new tab but keep the focus on quick view windowv: Open the file in a vertical splitgv: Open the file in a vertical split but keep the focus on quick view window
Greplace
Search and replace with greplace:
:Gsearch: Start the search and replace- Specify the term and path to search
- Update all found occurrence one by one, or simply using:
:%s/term/replacement/g Greplace: To actually replace the matching term, selectato confirm all:waSave all buffers
Tabs and Indentations
tabstop: Specify the width of tab character (default8)expandtab: Use spaces instead of tab characters (defaultnoexpandtab)softtabstop: Fine tune the amount of whitespace to be inserted (default0)shiftwidth: Defines the amount of whitespaces to be inserted or removed when using the indentation command on normal mode (default8):retab!: Convert the tab according toexpandtabornoexpandtabconfig.>: Increase indentation 1 level<: Decrease indentation 1 level=: Auto-indent- Go to top
ggand press=Gto auto indent file =i}: Auto indent block of code within the curly braces:{ . . . }
Vim Surround
cs'": Change the surrounding single quote with double quote.cs({: Change the surrounding bracket with square bracket.ds': Delete the surrounding single quotecstthen enter<strong class="foo">: change the surrounding tag with<strong>- Select the text and enter
Sthen<span>to surround it with<span>tag.
Macros
qx: Start recording toxregister, pressqagain to stop:reg: List the registers@x: Play the recording onxregister'xp: Split out the typed keys fromxregister"xp: Split out the typed keys fromxregister, including the “going to insert mode” key and others.
FZF
CTRL+T: Fuzzy search the current directoryCTRL+R: Fuzzy search the history commandsCTRL+j: Move the selection downCTRL+k: Move the selection uprm <CTRL+T: Fuzzy search files forrmcommand (presstabto select file)cd**: Trigger fuzzy search forcdcommand
tmux
Sessions:
tmux ls: List all tmux sessiontmux: Start a new tmux sessiontmux new -s foo: Start a new tmux session with named footmux attach -t foo: Attach to the tmux session named footmux switch -t foo: Switch to the tmux session named footmux detachor<prefix> d: Detach from the current session
Windows:
<prefix> cortmux new-window: Create new window<prefix> 0ortmux select-window -t 0: Go to the 1st window<prefix> 2: Go to the 3rd window<prefix> xortmux kill-window: Close the current window<prefix> ,ortmux rename-window: Rename the current window
Panes:
<prefix> "ortmux split-window: Split window horizontally<prefix> %ortmux split-window -h: Split window vertically<prefix> up/down/left/right keyortmux select-pane -[UDLR]: Switch to pane