vim
Resources
Load vim with no plugins enabled
vim -u NONE -N
Delete without overriding your last yank
All yank and delete operations write to the unnamed register by default. However, the most recent yank and most recent delete are always stored (separately) in the numbered registers. The register 0 holds the most recent yank. The registers 1-9 hold the 9 most recent deletes (with 1 being the most recent).
In other words, a delete overwrites the most recent yank in the unnamed
register, but it's still there in the 0 register. The blackhole-register trick
("_dd
) mentioned in the other answers works because it prevents overwriting
the unnamed register.
You reference a register using double quotes, so pasting the most recently yanked text can be done like this:
"0p
This is an excellent reference:
Commenting lines
Using the vim-commentary
plugin:
gcc comment out a line
gc comment out the target of a motion
gcap comment out a paragraph
Surrounding
Surrounding words with quotes, brackets, XML tags and more can be done with
the vim-surround
plugin.
cs"' change the surroundings from " to '
cs' delete the ' surroundings
ysiw] change surroundings to ]
Substitute multiple variants of a word
With vim-abolish
plugin installed:
:%Subvert/facilit{y,ies}/building{,s}/g
Faster actions
Use the vim-unimpaired
plugin to
quickly access pairs of mappings (new lines, next files, etc.)
Marks
Marks are a dope way to navigate between files or within a file. If Ctags
aren't working as planned, having marks is pretty neat. It's also neat when
there's a large file in which you have to jump between sections (and (
and )
won't do).
m<char> set mark at current cursor position
ma set mark at a, bound to file
mA set mark to A, bound to project
'a jump to location of mark a
`a jump to position (line and column) of mark a
:marks list all current marks
Stash vim session
In vim do C-z
to stash the window in the background. To pop it run:
$ fg
Undo trees
With gundo installed:
GundoShow
Delete all lines that match a pattern
:g/<regex>/d
Stop using hjkl
Using hjkl is an antipattern as it's slow. Instead use the other motions.
Set line numbers
:set nu "enable numbers
:set nonu "disable number
:set nu! "toggle
Sort lines
:sort u "alphabetically
Split windows
Cw-s "split window horizontally
Cw-v "split window vertically
Buffers
Buffers are often a misunderstood / underused feature of vim, even though they
are essential to creating a fast workflow. Many editors use tabs to manage open
files, but with vim
it's recommended to use buffers for this. Vim also has
tabs, but they're used differently.
tabs " open one per feature; workspaces
buffers " open one per file, nested under a workspace
Tabs can be used as different views on buffers. Buffers are used to manage
files. By themselves buffers are a bit tedious to work with, but by using
ctrlp
they become more usable
through fuzzy finding.
A good mapping for ctrlp
is:
map <Leader>b :CtrlPBuffer<CR> " Where leader is ideally set to spacebar
buffer commands
enew " open an empty buffer
bd " close a buffer
<bufno> bd " close a specific buffer
ls " list open buffers
ls! " list all open buffers (including unlisted)
b <bufno> " open buffer number
b# " open last buffer (or <C-^> / <C-6>)
Visual block mode
Visual block mode is useful for bulk changes to patterns that are hard to capture with a regex; usually columns.
C-v " enter visual block mode
A " enter insert mode
I " enter insert mode
esc " execute changes made in insert mode
Format text to fit lines
With :set tw=80
do:
gq
Jump to character
f<char> " jump to character
F<char> " jump back to char
t<char> " jump to char before match
; " repeat jump
, " repeat jump back
Folds
zc " close a fold
zC " close all folds
zo " open a fold
zO " open all folds
za " toggle a fold
zA " toggle all folds
Macros
q<key> record macro at <key>
q stop recording
@<key> play back macro
:<num> @<key> play back macro <num> time
Open url under cursor in browser
gx
Disable conceal
In 2011 vim added a feature called conceal
which allows syntax parts to be
shadowed, allowing "nicer" views. For some people (like me) it feels as if
reality is distorted, and want to disable this. Luckily that's possible through
a setting:
set cole=0 " disable conceal
set cole=2 " enable conceal
Stop wrapping lines
Or doing linebreaks
:set nowrap
Autocommands
Sometimes you wanna script a thing when you create a new file or start editing one; you can trigger stuff using autocommands:
autocmd BufNewFile,BufReadPost *.md :Goyo