Vim is a Command Line Interfacetext editor.

Famous StackOverflow answer on learning Vim: Your problem with Vim is that you don’t grok vi

Terminology

  • Tabs: containers for windows
  • Windows: to view buffers
  • Buffers: act as file proxies (1-to-1) and arglist viewer

Command line

Start Vim with no config

vi -u NONE

Start Vim with vertical splits

vi -O <filename>

Movement

Navigation in buffers:

KeyMovement
<CTRL-U>Up half a screen
<CTRL-D>Down half a screen
HTop (high) of screen
MMiddle of screen
LEnd (low) of screen
ztPut cursor to top
zzPut cursor to middle
zbPut cursor to bottom
<CTRL-Y>Up (Backwards) a line
<CTRL-E>Down (Forwards) a line
<CTRL-B>Up (Backwards) a full screen
<CTRL-F>Down (Forwards) a full screen

Modes

ModeSample Keystrokes
Normal ModeEsc
Insert Modei, a, c
Visual Modev, V, <Ctrl-v>
Command-line Mode:, /

Operators

cchange
ddelete
yyank in to register
~switch case
gumake lowercase
gUmake uppercase
!filter to external program
<shift left
>shift right
=indent

Text Objects

Key PressedText Object Selected
awa word
iwinner word
aWa WORD
iWinner WORD
apa paragraph
ipinner paragraph
aba bracket
ibinner bracket
ata tag block
itinner tag block

NB. Capitals skip punctuation

Motions

Key PressedAction Performed
%go to first matching paren/bracket
[count]+down to first non-blank char of line
[count]$to end of line
[count]f/F{char}to next occurance of {char}
[count]t/T{char}to before next occurance of {char}
[count]h/j/k/lleft, down, up, right
[count]]mto beginning of next method
[count]w/Wgo a word / WORD to the right
[count]b/Bgo a word / WORD to the left
[count]e/Ego to end of word / WORD right

NB. Capitals skip punctuation

Examples of combining count, operator, object and motion

Keys PressedAction Performed
6+6x go down to line start
gUaWcapitalize a WORD
3ce3x change to word end
4$4x end of line
d]mdelete to start of next method
%jump to next paren./bracket

Buffers & Arglist

Buffers are for everything you open in a Vim session. Arglist is the files Vim was supplied with when opened first.

KeyAction
<CTRL-^>Switch buffers
:bnNext buffer
:b {filename}go to buffer with {filename}
:bdDelete buffer
:buffersShow all buffers
:ngo to next file (based on arglist)
:arga {filename}Add {filename} to arglist
:argl {files}male a local arg copy via {files}
:argsshow all args on arglist

Clipboard

From normal mode (press ESC to enter):

KeyAction
vStart selection (Enables [v]isual mode)
<Shift-v>Start line selection
<CTRL-v>Start block selection
yyyank (copy) current selection
Ppaste before
ppaste after
:registerssee what is stored to be pasted

Searching

is carriage return (aka. Enter key).

KeyAction
/{patt}[/]<CR>search for {patt}
/<CR>search for last used pattern
?{patt}[?]<CR>search backwards for {patt}
?<CR>search backwards for last used pattern
[count]nrepeat search [count] times
[count]Nrepeat search backwards [count] times
*search forward for word under cursor
#search backward for word under cursor
gdgo to local declaration
:hls!toggle search highlighting

Marks

is carriage return (aka. Enter key).

KeyAction
m{a-zA-Z}set a mark at a char of {a-z} or {A-Z}
`{a-zA-z}jump to mark set at char
:marksshow all marks set
`.jump to last change

Tags

KeyAction
g<CTRL-]>Show definitions
<CTRL-]>Jump to keyword definition
<CTRL-t>pop from tag stack

Jumplist & Changelist

Jumplist:

KeyAction
<CTRL-O>Cycle through jumps (backwards)
<CTRL-I>Cycle through jumps (forwards)
:jumpsList all jumps

Changelist:

KeyAction
g;Cycle through changes
g,Cycle through changes
:changesList all jumps

Quickfix list:

KeyAction
:cnNext on QuickFix list
:cnPrevious on QuickFix list

Windows

Windows are viewports for buffers

KeyAction
<Ctrl-w> sSplit window
<Ctrl-w> vSplit window vertically
<Ctrl-w> qClose window
<Ctrl-w> wMove cursor to alternate windows
<Ctrl-w> oMake buffer only one in window
<Ctrl-w> xSwap windows
<Ctrl-w> rRotate windows
:windo {CMD}Execute command for all windows
:sf {FILE}split window and find file
:vert {CMD}Make any split {cmd} be vertical

Tabs

Tabs are collections of windows.

KeyAction
gtGo to next tab
gTGo to previous tab
:tabcClose Tab
:tabeOpen Tab
:taboClose all other tabs

Vim as an IDE

KeyAction
:20vs .Use netw to view files in 20 char split
:find <filename>Can find and open <filename>
:vert sf <filename>split a search for <filename>, open in split
<CTRL-W> xswap windows
:tabf <filename>open tab with <filename> (not affect existing win)
:vert sf <filename>split a search for <filename>, open in split
pOn <filename> in netrc, will preview
<Ctrl-w> zClose preview window
<Ctrl-w> oMake buffer only one in window
:argsSee what vim was opened with
:args */.yamlMake arglist all YAML files
:sallSplit all files in arglist
:vert saVertically split all files
:windo difftdo a diff for every file in window
<CTRL-X> 
<CTRL-L> 
gTgo back to previous tab
gtgo to next tab
:vimgrep TODO %find all todos in current file
:cnnext on quickfix list
@repeat command
:args */.pyadd all python files to arglist
:vim TODO ##vimgrep through all on argslist
:cncan see all files on arglist are search and move
:cdo s/TODO/DONE/greplace all TODOs with DONE on every line
:cpcan see previous in QuickFix (all are changed)

See also: Emacs