Vim

Vim is a highly configurable text editor

Verbs

Verb Description
v Visual
c Change
d Delete
y Yank
m Mark

Modifiers

Modifier Description
i Inside
a Around
t Till (stop before)
f Find (put cursor on)

Nouns

Noun Description
w Word
s Sentence
p Paragraph
b Block/Parentheses
t Tag

Searching/Navigating

Command Description
f{c} Jump forward and put cursor on {c}
F{c} Jump back and put cursor on {c}
t{c} Jump forward and put cursor before {c}
T{c} Jump back and put cursor after {c}
/{s} Search for {s}
/{s}/e (In visual mode) Select text up to and including {s}
* Search for other instances of the word under cursor
n Next occurrence
N Previous occurrence
0 Go to beginning of line
$ Go to end of line
^ Go to first non-blank character in line
_ Go to first non-blank character in line
w Move forward one word
b Move back one word
e Move to the end of your word
gg Go to top of file
G Go to bottom of file
<C-i> Previous location
<C-o> Forward location
:{l} Move to line number {l}
% Go to block close

Registers

Command Description
"${register} Access the content in the register (e.g. "a)
"${register}y Yank into the register (e.g. "ay)
"${register}p Paste from the register (e.g. "ap)
<C-r> ${register} Paste from the register in insert/command mode (e.g. <C-r> a)

Editing

Command Description
i Insert before cursor
a Append after cursor
I Insert at the beginning of line
A Insert at the end of line
o New line below
O New line above
r Replace character
R Replace but keep typing
C Change rest of line
~ Change case
x Delete under cursor
X Delete before cursor
dd Delete line
D Delete to end of line
J Join the current line with the next
u Undo
<C-r> Redo
. Repeat command
yy Yank line
{d}yy yank {d} lines
p Paste after cursor
P Paste before cursor
edit! Reload buffer
<n>df<x> Delete n number of occurences of x

Macros

Command Description
q{c} Record macro and store in {c}
q Stop recording
@{c} Replay macro stored in {c}
:5,10norm! @{c} Execute the macro stored in {c} on lines 5 through 10
:5,$norm! @{c} Execute the macro stored in {c} on lines 5 through the end of the file
:%norm! @{c} Execute the macro stored in {c} on all lines
:g/pattern/norm! @{c} Execute the macro stored in {c} on all lines matching pattern
:'<,'>norm! @{c} Execute the macro stored in {c} on visually selected lines

Search and Replace

Command Description
:%s/foo/bar/g Find each occurrence of foo (all lines), and replace it with bar
:s/foo/bar/g Find each occurrence of foo (current line), and replace it with bar
:%s/foo/bar/gc Change each foo to bar, but ask for confirmation first
:%s/\/bar/gc Change only whole words exactly matching foo to bar; ask for confirmation
:%s/foo/bar/gci Change each foo (case insensitive) to bar; ask for confirmation
:%s/foo/bar/gcI Change each foo (case sensitive) to bar; ask for confirmation
:'<,'>s/\%Vfoo/bar/g Change each foo to bar in visual selection
:'<,'>s/\%V\s/,/g Change each space to , in visual selection

Splits

Command Description
:vsp Vertical split
:sp Horizontal split
:resize 60 Change height of window
:res +5 Change height of window
:res -5 Change height of window
:vertical resize 80 Change width of window
:vertical resize +5 Change width of window
:vertical resize -5 Change width of window

Global Command

The global command is used to run a command-line command on multiple lines simultaneously. The syntax is :g/pattern/command

Command Description
:g/pattern/d Delete all lines containing pattern
:g!/pattern/command Delete all lines not containing pattern
:g/one\|two/d Delete all lines containing either one or two
:g/[0-9]/d Delete all lines containing any single digits (alternative is :g/\d/d)
:g/./normal A; Add a ; to the end of each non-empty line
:g/pattern/execute "norm Ahello" Add hello to the end of each line containing pattern

Terminal

Command Description
:term Open terminal
:vsp | term Open terminal in a vertical split
:sp | term Open terminal in a horizontal split

Tips and Tricks

Command Description
:noautocmd w Save file without executing Autocommand events
"_d Use “black hole register” and delete
:map Show mappings
:map <leader>rn Show mapping for <leader>rn
:map j Show mapping for j
:map <F5> Show mapping for <F5>
:help key-notation Display key notation-meaning
:e /path/to/file Open file
nvim -u /path/to/file Use a custom vimrc file
export XDG_CONFIG_HOME="~/.config/minimal" Use a custom init.vim file (it looks for $XDG_CONFIG_HOME/nvim/init.vim)
vim --clean -u DEFAULTS -U NONE -i NONE Run vim without any settings
%s/\s\+$//e Delete any trailing white space at the end of each line

Examples

Command Description
d2w Delete two words
cis Change inside sentence
100i{s} Writes {s} one hundred times
3. Repeat command three times
:%!uniq Remove duplicate lines
:%!jq . Prettify a JSON file
{d}df{x} Deletes up to the {d}th character of {x}
$A, Add a comma to the end of the line
:g/^$/d Delete empty lines
:g/price/d Delete lines that contain the word “price”
:g!/price/d Delete lines that DON’T contain the word “price”
:v/price/d Delete lines that DON’T contain the word “price”
verbose set shiftwidth Find where a variable was last set (in this case set shiftwidth)
q: Open ex-command history in a vim buffer
:e existing/path/new_file.ext Create new file

Use argdo to update multiple files (in this case, set shiftwidth to 4 and then update the indenting with gg=G)

:args ~/path/to/project/**/* | argdo execute "set shiftwidth=4" | execute "normal gg=G" | update

Use norm mode to update multiple lines at once.

:norm 0f,x10i

# 0	-> Go to beginning of line
# f,	-> Go to first , symbol
# x	-> Delete the , symbol
# 10i 	-> Insert ten spaces (note there is a space after 10i)
Negative lookahead

Given the following input, how do we match the lines that have abc but do not also contain xyz somewhere else on the same line (i.e. it doesn’t need to immediately precede it) -> /abc\(.*xyz\)\@!

The abc is the best // Should match
The first three letters are abc // Should match
The abc is the best but xyz is cheaper // Should not match
The first three letters are abc and the last are xyz // Should not match
Using Ack and cdo

Using the Ack package and built-in :cdo command, you can replace all instances of foo with bar easily:

:Ack foo
:cdo s/foo/bar/g | update
Yank and paste while incrementing
qaVypt)<C-a>q135@a
  • qa - begins the recording of a macro inside the register a (you could choose another letter).
  • Vyp - duplicates the current line, by selecting the whole line (V), copying it (y) and then pasting it below (p).
  • t) - move the cursor just before the next closed parenthesis, so that it’s on the second number.
  • <C-a> - (Ctrl and a) increments the number.
  • q - ends the recording of the macro.
  • 135@a - replays the macro stored in the register a 135 times.