Index

Table of contents

vim

cli

start vim
vim
vim [file]...
start vim in read only mode
vim -R [file]...
view [file]...
pipe output of another command to vim
ls -la | vim -
open multiple files in horizontal panes
vim -o [file]...
open multiple files in vertical panes
vim -O [file]...
edit remote file via scp
vim scp://user@host//path
open a file on a specific file number
vim [file] +[line]
execute vim command on startup
vim '+[cmd]'
vim '+find **/Main.java'
load .vimrc from another location
vim -u [file]

environment variables

environment variable for relocating the vimrc
MYVIMRC=~/.vimrc

vimdiff

view diff between 2 files
vimdiff [file-a] [file-b]
gvimdiff [file-a] [file-b]
more info
https://vimhelp.org/usr_08.txt.html#08.7
https://vimhelp.org/diff.txt.html#vimdiff

insert mode

go to normal mode
esc
ctrl-[
ctrl-\, ctrl-n    (immediate)
execute a single command in normal mode, then return to insert mode
ctrl-o
delete word before cursor (works in bash as well)
ctrl-w
delete all preceding characters on line (works in bash as well)
ctrl-u
indent line left
ctrl-d
indent line right
ctrl-t
insert character from line above
ctrl-y
insert character from line below
ctrl-e
auto complete
ctrl-p            previous
ctrl-n            next
ctrl-x, ctrl-f    filename based auto complete
ctrl-x, ctrl-k    dictionary based auto complete
ctrl-x, ctrl-l    line based auto complete
ctrl-x, ctrl-o    omni completion (good for css)
ctrl-e            abort auto complete
ctrl-y            accept auto complete
paste from selection clipboard
ctrl-r, *
paste from system clipboard
ctrl-r, +
more general
ctrl-r, [register]
define abbreviation
:ab [abbreviation] [replacement]
:abbreviation [abbreviation] [replacement]

digraphs

insert digraphs
ctrl-k a '      á
ctrl-k a "      ä
ctrl-k a :      ä
ctrl-k a `      à
ctrl-k a ~      ã
ctrl-k a ^      â
ctrl-k ? I      ¿
ctrl-k s s      ß
ctrl-k R g      ®
ctrl-k E u      €
greek alphabet
ctrl-k a *      α Α
ctrl-k b *      β Β
ctrl-k c *      ξ Ξ
ctrl-k d *      δ Δ
ctrl-k e *      ε Ε
ctrl-k f *      φ Φ
ctrl-k g *      γ Γ
ctrl-k h *      θ Θ
ctrl-k i *      ι Ι
ctrl-k j *      ϊ Ϊ
ctrl-k k *      κ Κ
ctrl-k l *      λ Λ
ctrl-k m *      μ Μ
ctrl-k n *      ν Ν
ctrl-k o *      ο Ο
ctrl-k p *      π Π
ctrl-k q *      ψ Ψ
ctrl-k r *      ρ Ρ
ctrl-k s *      σ Σ
ctrl-k t *      τ Τ
ctrl-k u *      υ Υ
ctrl-k v *      ϋ Ϋ
ctrl-k w *      ω Ω
ctrl-k x *      χ Χ
ctrl-k y *      η Η
ctrl-k z *      ζ Ζ
math
ctrl-k 1 2      ½
ctrl-k 2 3      ⅔
ctrl-k 2 s      ₂  # subscript
ctrl-k 2 S      ²  # superscript
ctrl-k 2 2      ²  # superscript
ctrl-k i e      є
ctrl-k j 3      ϵ
ctrl-k ( -      ∈
ctrl-k 0 0      ∞
ctrl-k = 3      ≡
ctrl-k ? 2      ≈
ctrl-k ! =      ≠
ctrl-k = <      ≤
ctrl-k > =      ≥
ctrl-k . M      ·
ctrl-k R T      √
ctrl-k I n      ∫
ctrl-k I o      ∮
ctrl-k + Z      ∑
ctrl-k D E      ∆
ctrl-k N B      ∇
ctrl-k d P      ∂
ctrl-k - V      ∠
ctrl-k - T      ⊥
arrow
ctrl-k - >      →
ctrl-k - <      ←
ctrl-k < -      ←
ctrl-k < >      ↔
ctrl-k = >      ⇒
ctrl-k < =      ⇐
ctrl-k = =      ⇔
ctrl-k U D      ↕
ctrl-k U D      ↕
ctrl-k - !      ↑
ctrl-k - v      ↓
ctrl-k - !      ↑
ctrl-k - v      ↓
figure
ctrl-k h h      ─
ctrl-k v v      │
ctrl-k P P      ∥
ctrl-k ! 2      ‖
ctrl-k D b      ◆
ctrl-k 0 M      ●
ctrl-k f S      ■
digraph help
:help digraph

visual mode

copy selected block
y
delete selected block
d
paste block
p
jump to other end of selection
o
O
insert string on all selected lines (block mode)
I[string]<esc>
append string on all selected lines (block mode)
A[string]<esc>
replace block (until end of block)
c[string]<esc>
replace block (until end of line)
C[string]<esc>
switch case for all characters in block
~	inverse
U	upper
u	lower
block ident (from start of block, not start of line)
<
>
join slected lines
J
reselect last visual selection
gv

normal mode

command prompt

clear prompt
ctrl-U
auto complete prompt
<tab>     # cycle options
ctrl-L    # complete unambigious part only
show available content completion options for prompt
ctrl-D
execute multiple commands in a row (:help :bar)
:[cmd1] | [cmd2]
show history
:history      # command history
:history /    # search history
edit and run history
q:

moving around

basic movement
h left
j down
k up
l right
viewport based movement
L      jump to lowest line in viewport  [n] -> nth from bottom
M      jump to middle line in viewport
H      jump to highest line in viewport [n] -> nth from top
ctrl-E move viewport up a line
ctrl-Y move viewport down a line
ctrl-U move viewport up half a screen
ctrl-D move viewport down half a screen
ctrl-F <pgup>
ctrl-B <pgdn>
pgup   move viewport up one screen
pgdn   move viewport down one screen
z<cr>  start viewport on current line
z.     center viewport on current line (reset column)
zz     center viewport on current line (remember column)
z-     end viewport on current line
word based movement
w      next word [a-z0-9_]
W      next word \S
b      back one word [a-z0-9_]
B      back one word \S
e      next word end [a-z0-9_]
E      next word end \S
ge     previous word end [a-z0-9_]
gE     previous word end \S
line based movement
0      line start
^      line start
<HOME> line start
$      line end, [n]$ for nth line
<END>  line end, [n]$ for nth line
<CR>   move to start of next line (after whitespace)
+      move to start of next line (after whitespace)
-      move to start of previous line (after whitespace)
=      move to start of current line (after whitespace)
}      next blank line
{      previous blank line
:[n]   jump to line [n]
[n]G   jump to line [n]
[n]gg  jump to line [n]
f[c]   jump forward to character [c], [n]f[c] for nth match
F[c]   jump backward to character [c], [n]f[c] for nth match
t[c]   jump forward to character before [c], [n]f[c] for nth match
T[c]   jump backward to character before [c], [n]f[c] for nth match
display line based movement (wrapped lines)
gj     next display line
gk     previous display line
g0     start of display line
g$     end of display line
method based movement
]m    jump to begin of next method
]M    jump to end of next method
[m    jump to begin of previous method
[M    jump to end of previous method
file based movement
% jump to matching parenthesis for selected or closest ()[]{}
gg   move to first line
G    move to last line
[p]% move to [p] percent of file
history based movement
``          move back to position preceding the last jump
ctrl-O      go back in jump history (older)
ctrl-I      go forward in jump history (newer)
:jump       show jump history
marks
m[a-z]      mark a local position (current file)
m[A-Z]      mark a global position (any open file)
'[a-z]      jump to mark, beginning of line
`[a-z]      jump to mark, marked line & column
`'          jump to line before jump
``          jump to line before jump
`"          jump to position when last editing file
`.          jump to last change
`^          jump to last insert
`[          jump to start of last change or yank
`]          jump to end   of last change or yank
`<          jump to start of last visual selection
`>          jump to end   of last visual selection
:marks      list marks
changes
:changes    show change list
g;          traverse change list backwards
g,          traverse change list
gi          resume insert mode in last insert mode location
view history of files edited
:browse oldfiles
open last file edited
'0
open man page for word under cursor
K

editing

scroll one line (up/down)
ctrl + Y
ctrl + E
go to insert mode on cursor
i
go to insert mode on line start
I
go to visual mode (lines)
v           lines starting at cursor
V           whole lines starting at current line
ctrl + v    columns
add new line after current and edit it (insert mode)
o
add new line before current and and edit it (insert mode)
O
delete line from here and go to insert mode
C
delete line and go to insert mode
S
delete one line
dd
delete n lines
[n]dd
d[n]d
delete from begin of line to here
d^
d0
delete from here to end of line
D
d$
delete from here to end of word
dw
delete focused word
diw    whitespace excluded
daw    whitespace included
delete sentence ending with "."
d)
df.
delete paragraph
dip
dap
delete inbetween quotes
di'
di"
delete block, valid characters: [], (), {}
d%
delete code block
d{
d}
html tags
dit dat
deleting single characters
x    delete character under cursor
X    delete character before cursor
[n]x delete [n] characters (forward)
[n]X delete [n] characters (backwards)
copy one line (where the cursor is)
yy
Y
copy from cursor to end of line
y$
paste before cursor
P
paste after cursor
p
paste [n] times
[n]p
paste from system clipboard
<shift> + insert
letter toggle case
~
word toggle case
g~w    switch case
gUw    upper case
guw    lower case
line toggle case
g~~    switch case
gUU    upper case
guu    lower case
undo
u
redo
ctrl-r
indent current line
>>
<<
indent n lines
[n]>
[n]<
shift left until end of file
<G
increment first number to the right
ctrl-a
decrement first number to the right
ctrl-x
repeat last (change) command
.
generate HTML for current file with syntax highlighting
:TOhtml

working with files / buffers

quit
:q
force quit
:q!
save file and quit
:x
new empty buffer
:new
:vnew
open file in editor (new buffer)
:e(dit) [file]         warn if currently open file has changes
:e(dit)! [file]        silently discard changes in currently open file
:hide e(dit) [file]    ignore changes in currently open file and open a new buffer
reload current file
:e!
search for a single file and open it in buffer, fail on multiple matches
:e **/Main.java
:e `find .. -name 'Main.java'`
:e `find .. \| grep todo`
search for multiple files and open every one in a separate buffer
:arg *.txt
:arg *.txt *.log
:arg ../**/vim.txt
:arg `find -type f -name 'vim*'`
save file
:w(rite)
save as
:w(rite) [file]
append to file (must exist)
:w(rite) >> [file]
overwrite file
:w(rite)! [file]
write range of lines to file
:[n],[m]w(rite) [file]
change default file for write command
:file [file]      # do not writeback to disk
:saveas [file]    # writeback to disk
show open files / buffers with indexes
:buffers
:ls
switch to open file / buffer
:b [index]
cycle buffers
:b <tab>...
next unread buffer
:n
:next
:wn
:wnext
:n!
:next!
next buffer
:bn
previous buffer
:bp
close buffer
:bd
:bd [index]
wipeout current buffer (no save warning)
:bw
show jump history
:jumps
history: jumpt to previous edit location
ctrl-o
history: jumpt to next edit location
ctrl-i
run an ex-command on all open buffers
:bufdo [cmd]
example: remove trailing whitespace from all open buffers
:bufdo %s/\s\+$//
run an ex-command on all open windows (split panes)
:windo [cmd]
show arg list
:args
run an ex-command on arg list only
:argdo [cmd]

find command and related properties

go to file under cursor
gf          same window
ctrl-w f    new window
comma separated list of suffixes to append automatically on file search
:set suffixesadd=.java, .py
find file in (vim) path entries
:find src/main/java/Main.java
:find **/Main.java
extend path for file searches
:set path+=**
:set path+=~/**
:set path+=./more
:set path+=/tmp/path

built-in filebrowser

open file browser
:Explore
:Sexplore
:Texplore
:Vexplore
:E
:e .
:e [directory]
search for location and open in file browser
:e `find . -type d -name vim`
default file browser commands
-         navigate up one level
%         create file
d         create directory
D         delete directory
cd        make browsing directory the current directory
i         cycle view modes
I         toggle banner
o         horizontal split on file / directory
p         preview file
r         reverse order
R         rename
s         change sort mode
v         vertical split on file / directory
x         execute with associated program
/         search through listed files
<Del>     delete file / directory
documentation page for built-in filebrowser
:help netrw
all keyboard shortcuts for built-in filebrowser
:help netrw-quickmap
blogpost on customizing netrw
https://shapeshed.com/vim-netrw/

search and replace

search

find other matches of word under cursor
# backward search exact match
* forward search exact match
g* forward search partial match
g# backward search partial match
find (use c-r, c-w to autocomplete from partial match)
/[regex]
reverse search
?[regex]
next search result
n
previous search result
N
select next search result in visual mode
gn
select previous search result in visual mode
gN
better regex search
/[regex]\v
literal search
/[regex]\V
case insensitive search
/[regex]\c
case sensitive search
/[regex]\C
position cursor at end of match
/[regex]/e
find slash forward
/\/
find slash backward
/\\
find with wildcard matching
/.*
/.\+
character class
/[^a-z]
literal dot
\.
[.]
any character
.
word character
\w
non-word character
\W
word boundary
/\<
/\>
capturing group
(...)
non-capturing group
%(...)
match start
\zs
match end
\ze
documentation
:help s_flags
:help pattern-overview
:help character-classes
:help perl-patterns

replace

general form
:[range]s /search/replace/[flags]
replace first occurrence on current line
:s /search/replace/
replace first occurrence on any line
:%s /search/replace/
replace all occurrences on any line
:%s /search/replace/g
replace confirmation mode
:%s /search/replace/c
show count only
:%s /search/replace/n
reuse flags from last substitute command
:%s /search/replace/&
refer to capturing group 0 (entire line)
:%s /search/pre\0post/
:%s /search/pre&post/
refer to first capturing group
:%s /before\(keep\)after/pre\1post/
reuse last replacement string
:%s /search/~/
use vim expression in replacement
:%s /search/\=[expr]/
repeat last substitution on entire file
g&
repeat last substitution
:[range]&    don't remember flags
:[range]&&   remember flags
example: switch all occurrences of 2 words
:%s/\v(foo|bar)/\={"foo":"bar","bar":"foo"}[submatch(1)]/g

command ranges

line [n] to [m]
[n],[m]
current line
.
current line + [n] more lines
.,.+[n]
current line to end of document
.,$
start of document to current line
0,.
find a line matching [regex]
?[regex]?    # backward search, first match
/[regex]/    # forward search, first match
current line up to line matching [regex]
.,/[regex]/
from line matching [regex1] up to line matching [regex2]
/[regex1]/,/[regex2]/
?[regex1]?,/[regex2]/
?[regex1]?,?[regex2]?
find a line matching [regex] with offset of [n] lines
/[regex]/-[n]
/[regex]/+[n]
using marks
'[a-z]
'[a-z],'[a-z]
some ex-commands (full list :help ex-cmd-index)
[range] delete     [register]?
[range] yank       [register]?
[line]  put        [register]?
[range] copy       {address}
[range] move       {address}
[range] join
[range] substitute
[range] global
[range] normal     {cmd}         (short form: norm)
example: delete lines 3-5
:3,5d
example: yank lines 3-5
:3,5y
example: write line [n] to [m] to a new file
:[n],[m] write [file]
example: replace on lines [n] up to [m]
:[n],[m]s/[search]/[replace]/
example: delete this line and the next [n] lines that follow
:.,.+[n]d
documentation
https://vimhelp.org/usr_10.txt.html#10.3

global

syntax of the global command
:[range] g[lobal] /{pattern}/ [cmd]
execute command on all lines matching a regex
:g/[regex]/[command]
:g|[regex]|[command]
:g:[regex]:[command]
:g=[regex]=[command]
:g+[regex]+[command]
execute command on all lines NOT matching a regex
:v/[regex]/[command]
:g!/[regex]/[command]
delete all lines matching a regex
:g/[regex]/d
copy all lines matching a regex to the end of the document
:g/[regex]/t$
move all lines matching a regex to the end of the document
:g/[regex]/m$
move all lines matching a regex to line [n]
:g/[regex]/m[n]
on lines matching [regex] search for [query] and replace with [replacement]
:g/[regex]/s/[query]/[replacement]/g
on lines matching [regex] execute some keys as if they were typed in normal mode
:g/[regex]/ normal [keys]
example: put a newline before every line
:g/^/pu = \"\n\"
example: delete all empty lines
:g/^$/d
example: delete all blank lines
:g/^\s*$/d

shell command

run shell command (e.g. ls)
:![cmd]
run shell command and insert command output into document
:read ![cmd]
:r ![cmd]
run shell command with contents of document (piped)
:write ![cmd]
:w ![cmd]
:%![cmd]
pipe current line to shell command
:.![cmd]
!![cmd]      will automatically be replaced with :.![cmd]
run command and insert command output into vim
:read ![cmd]
run command and insert command output after line number [n]
:[n]read ![cmd]
run command and insert command output at end of document
:$read ![cmd]
run command and insert command output before first line
:0read ![cmd]
run command and overwrite first line
:0 ![cmd]
process range with shell command
:[n],[m]![cmd]
examples:
:[n],[m]!sort
:[n],[m]!sed s/search/replace/
:%!jq .
:%!tidy -i -q --wrap 0 --show-errors 0

tabs

open a file in a new tab
:tabedit [file]
open file browser in new tab
:Texplore
next tab
gt
c-pgup
previous tab
gT
c-pgdn
open tab number [n]
:[n]gt
close tab
:tabclose
close all other tabs
:tabonly
list open tabs
:tabs
open vim with multiple files in tabs
vim -p [file]...

split screen

split editor horizontal
Ctrl-W, s
:sp
:split
split editor vertical
Ctrl-W, v
:vsp
:vsplit
split and open file
:sp     [file]
:split  [file]
:vsp    [file]
:vsplit [file]
specify a destination for a split command
:leftabove  [cmd]
:aboveleft  [cmd]
:rightbelow [cmd]
:belowright [cmd]
:topleft    [cmd]
:botright   [cmd]
open terminal
:term
:terminal
:vert term
:vertical terminal
vim cycle split panes
Ctrl-W, direction
ctrl w, w
swap split panes
ctrl-w, r
close current window / tab
ctrl-w, c
:close
close current panel regardless of outgoing changes
:hide
write changes and close panel
ZZ
close all other split panes
:only
use full window height
ctrl-w _
use full window width
ctrl-w |
reset window widths & heights
ctrl-w =
increase window size
ctrl-w +
:res(ize) +1
:vertical resize +1
decrease window size
ctrl-w -
:res(ize) -1
:vertical resize -1
move between panes
ctrl w-h    # move left
ctrl w-j    # move down
ctrl w-k    # move up
ctrl w-l    # move right
reorder panes
ctrl w-H    # move left
ctrl w-J    # move down
ctrl w-K    # move up
ctrl w-L    # move right
quit (all panes)
:qall
save all panes
:wall
write all panes and quit
:wqall

registers

dump all registers
:display
:reg
:di
show specific register
:reg [register]
special registers
*    selection clipboard
+    system clipboard
_    black hole register (aka /dev/null)
"    unnamed register (default for copy paste actions)
0    yank register (not overwritten on deletes)
=    expression register (vim script expressions)
.    insert register (stores last insert)
:    last ex-command
/    last search (read-write)
yank to register
"[r]yy
"[r]Y
cut to register
"[r]dd
paste from register
"[r]p
echo register [r] contents
:echo @[r]
store a string in a register
let @a='test'
copy between registers
:let @[destination] = @[source]
example: copy register "a" to the system clipboard
:let @+ = @a
clear register
:call setreg('[r]', [])
execute ex-command stored in register "r"
:@r
print filename
:echo @%
:!echo %
file modifiers
:!echo %:e    file extension
:!echo %:h    head of file name (parent dir)
:!echo %:p    full path
:!echo %:r    base name
:!echo %:S    shell escape
:!echo %:t    (tail of) file name
:!echo %:~    path relative to home dir
:!echo %:.    path relative to current dir
substitution on file modifiers
:!echo %:s?[regex]?[replace]?    substitute
:!echo %:gs?[regex]?[replace]?   global susbstitution

macros

record macro in register [a-z]
q[a-z]
[commands]
q
append to macro
q[A-Z]
[commands]
q
execute macro in register [a-z] (as if the characters were typed in normal mode)
@[a-z]       # run once
[n]@[a-z]    # run [n] times
repeat the last executed ex-command
@:
repeat the last executed macro
@@

getting help

show help pages
:help
close help
helpc
show command index
:help index
show help for specific command
:help [cmd]
show help for vim command line argument (flag)
:help -[flag]
show help for key combination (e.g. CTRL-A) in command mode
:help [key]
show help for key combination (e.g. CTRL-A) in insert mode
:help i_[key]
show help for key combination (e.g. CTRL-A) in visual mode
:help v_[key]

navigation in help

follow link
ctrl-]
back
ctrl-o

spell checker

enable spellchecker
:set spell
load another dictionary
:set spelllang=en_us
:set spelllang=nl
disable check for capilization
:set spellcapcheck=
mappings for spell checking
[s    previous spell error
]s    next spell error
z=    show suggestions
zg    add word to dictionary
zw    remove word from dictionary
zug   undo last 'zg' or 'zw'

documentation

https://vimhelp.org/
http://vimdoc.sourceforge.net/htmldoc/vimindex.html
https://vim.fandom.com/wiki/Accessing_the_system_clipboard
vim documentation files
/usr/share/vim/vim80/doc