Vim Cheatsheet

Text

:set textwidth=79
to automatically insert lines breaks after 79 characters
gq
to wrap the text on the current line (or selected text)
:set wrap linebreak nolist
to display long lines as multiple screen lines

Spelling

:setlocal spell
turn on spell-checking
:set nospell
turn off spell-checking
:setlocal spell spelllang=en_gb
specify language
]s
move to next spelling mistake
zg
add the highlighted word to the dictionary
z=
suggest words for word under cursor

New files

:e filename
start editing a new file

or, in NERDTree, press m, and a from the context menu

Writing files

:sudowrite
write protected file (via eunuch plugin)
:sav
save a file with a new name, continue editing the new name
:w
save a file with a new name, continue editing the old name
:w %:h/filename
save filename in home directory of current file

Copy and Pasting

y
will yank the target into a buffer
p
will paste buffer after cursor
P
will paste buffer before cursor
ayy
copy to buffer a
ap
paste from buffer b
"+y
copy to OS copy buffer

Tabs

:tabnew
create a new tab
:tabclose
close tab
{i}gt
go to next tab
gT
go to previous tab
ctrl-w T
move window to new tab
:tabdo :command, e.g. %s/foo/bar/g
do command in all tabs

Folds

:set fdm=syntax

zc, zo, za
close, open, toggle fold at cursor
zC, zO, zA
close, open, toggle all folds at cursor
zr, zR
open one more (or all) level of folds throughout the buffer
zm, zM
close one (all) level of folds throughout the buffer

Windows

ctrl-w s
split horizontally
ctrl-w v
split vertically

CTAGS

,b
tagbar
,.
ctrlP browser
ctrl-}
navigate to tag under cursor
:tag <name>
navigate to named tag
:pop
pop navigation stack
:tnext, :tprev
navigate through multiple definitions

Comments

NERDCommenter is good for this

,c
(un)comment selection
,cc
comment current line
,cn
comment current line, force nesting
,ci
invert selection comments

Macros

q<letter>
start recording
q
finish recording
@<letter>
run macro
@@
repeat last macro

Editing macros

"<letter>p
paste macro buffer
0"<letter>y$
copy current line into macro

sed things

:g/profile/d
delete all lines containing “profile”
:g/^\s*$/d
delete all lines that are empty or that contain only whitespace:
:g!/^\s*"/d
delete all lines that do not contain a pattern, use g! or the equivalent v, like this command to delete all lines that are not comment lines in a Vim script: :v/^\s*"/d
:v/error\|warn\|fail/d
delete all lines except those that contain “error” or “warn” or “fail”

Surrounding things

cs "'
swap “” to ‘’
cs '<blah>
swap ‘ to
ci"
change all text within “”
ysiw *
surround the current word with *

Refactoring Ruby

:RAddParameter
Add Parameter
:RInlineTemp
Inline Temp
:RConvertPostConditional
Convert Post Conditional
:RExtractConstant
Extract Constant (visual selection)
:RExtractLet
Extract to Let (Rspec)
:RExtractLocalVariable
Extract Local Variable (visual selection)
:RRenameLocalVariable
Rename Local Variable (visual selection/variable under the cursor, REQUIRES matchit.vim)
:RRenameInstanceVariable
Rename Instance Variable (visual selection, REQUIRES matchit.vim)
:RExtractMethod
Extract Method (visual selection, REQUIRES matchit.vim)
:nnoremap <leader>rap  :RAddParameter<cr>
:nnoremap <leader>rcpc :RConvertPostConditional<cr>
:nnoremap <leader>rel  :RExtractLet<cr>
:vnoremap <leader>rec  :RExtractConstant<cr>
:vnoremap <leader>relv :RExtractLocalVariable<cr>
:nnoremap <leader>rit  :RInlineTemp<cr>
:vnoremap <leader>rrlv :RRenameLocalVariable<cr>
:vnoremap <leader>rriv :RRenameInstanceVariable<cr>
:vnoremap <leader>rem  :RExtractMethod<cr>

File greps with easygrep

<Leader>vv
Grep for the word under the cursor, match all occurences, like |gstar|
<Leader>vV
Grep for the word under the cursor, match whole word, like |star|
<Leader>va
Like vv, but add to existing list
<Leader>vA
Like vV, but add to existing list
<Leader>vr
Perform a global search on the word under the cursor and prompt for a pattern with which to replace it.
<Leader>vo
Select the files to search in and set grep options
<Leader>vy*
Invoke any option from the options explorer, where * is the shortcut for that option. e.g. <Leader>vyr - toggles recursion
<Leader>vyb
sets buffer grepping mode
Commands:
:Grep [arg]
Search for the specified arg, like <Leader>vv. When an ! is added, search like <Leader>vV

:Replace [target] [replacement]

:ReplaceUndo

Tagged: | vim | cheatsheet |
Cover Image: Ilya Pavlov, via Unsplash