Show Menu
Cheatography

Useful Vim commands Cheat Sheet by

Some useful Vim commands. Commands I sometimes need but often forget.

Copy/Paste

yiw
yank (inner) word
viwp
replace word with yanked
viw"0p
replace another word with yanked
ctrl-r +
paste from OS paste board

Moving

H / M / L
move to top/mi­ddl­e/b­ottom of screen
ctrl-d/u
move half screen down/up

Indenting

ctrl-t/d
Indent current line forwar­d/back in insert mode
</>
Indent block in visual mode (repeat with .)
:set shiftw­idth=4
Set indent to 4
:set tabstop=8
Set tab to 8
:set (no)ex­pandtab
Convert (space­s)tabs
:set pastet­oggle
change to paste mode
:set expandtab + set ts=4 + :%retab!
Convert tabs to four spaces in entire file
:set noexpa­ndtab + set ts=4 + :%retab!
Convert spaces to tabs in entire file

Folding

zM/zR
fold/u­nfold buffer
zc/zo
fold/u­nfold paragraph
zC/zO
fold/u­nfold all levels in paragraph
:set foldmethod=indent
fold by indent­ation
:set foldmethod=syntax
fold by syntax
 

Windows

n Ctrl-<>
dec/inc window width by n
n Ctrl-+/-
inc/dec window height by n
Ctrl-W=
make windows equal size
Ctrl-w s/v
split window horizo­nta­lly­/ve­rti­cally
Ctrl-w c
close window
Ctrl-w w
move to next window
Ctrl-w hjkl
move to hjkl window

Sessions

:mksession <fi­le>
Save session
:source <fi­le> or vim -S <fi­le>
Open session
vim --remote
Open file in existing window
vim --sess­ionname <na­me>
Create new session <na­me>
vim --serv­ername <na­me>
Open file in existing session <na­me>

Ctags / cscope

ctrl-]
jump to definition
ctrl-w }
show preview

Marks

ma
set local mark a
mA
set global mark A
'a
jump to line with mark a
`a
jump to position with mark a
 

Commands

:%!command
filter buffer through command
:w !sudo tee %
save file even if write protected
:%s//<­bar­>/g
replace every occurence of last search term
with <ba­r>
:%s/<c­-rw­>/<­foo­>/g
replace every occurence under cursor with <fo­o>
:g/pat­tern/d
delete lines containing pattern
:v/pat­tern/d
delete lines not containing pattern
:n,m ! cmd
filter lines n to m through shell cmd
:new|r ! cmd
run cmd, put output to new window
:set wrap linebreak nolist
set soft wrap
:set list
show spaces, tabs, newlines
:nohls­­earch
clear search highlights
:%!nl -ba
Add line numbers (different from :set number)

My mappings

,cc
comment code
,co
uncomment code
,c / ,v
copy/paste from persistent buffer
,a
Search for pattern with ack-grep
,A
Search word under cursor with ack-grep
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          VI Editor Cheat Sheet
          Vim NERDTree Cheat Sheet
          Helix Keyboard Shortcuts