Index

vim

insert mode

go to command mode
esc
auto complete
ctrl-n
paste from selection clipboard
control-r, *
paste from system clipboard
control-r, +
more general
control-r, [register]
execute a single command in normal mode, then return to insert mode
ctrl-o

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

command 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
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
M      jump to middle line in viewport
H      jump to highest line in viewport
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
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
line based movement
0     line start
^     line start
$     line end, [n]$ for nth line
<END> line end, [n]$ for nth line
}     next blank line
{     previous blank line
:[n]  jump to line [n]
[n]G  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
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, beginning of line
`"     jump to position when last editing file
`[     jump to start of last change
`]     jump to end of last change
`<     jump to start of last visual selection
`>     jump to end of last visual selection
:marks list marks
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 inbetween quotes
di'
di"
delete block, valid characters: [], (), {}
d%
delete sentence ending with "."
d)
df.
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
.
execute multiple commands in a row
:[cmd1] | [cmd2]

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
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
: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 history
:jumps
history: jumpt to previous edit location
ctrl-o
history: jumpt to next edit location
ctrl-i

search and replace

search

find other matches of word under cursor
* forward search exact match
 # backward search exact match
g* forward search partial match
g# backward search partial match
find
/[regex]
reverse search
?[regex]
next search result
n
previous search result
N
case insensitive search
/[regex]\c
case sensitive search
/[regex]\C
find slash forward
/\/
find with wildcard matching
/.*
/.\+
character class
/[^a-z]
any character
.
literal dot
\.
[.]
word boundary
/\<
/\>

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
refer to capturing group 0 (entire line)
:%s /search/pre\0post/
refer to first capturing group
:%s /before\(keep\)after/pre\1post/

vimgrep

searching multiple files at once
:vim     //g **/*.txt
:vimgrep //g **/*.txt
show next result
:cnext
show previous result
:clast
list all results
:cope

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]
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

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
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 with contents of document (piped)
:write ![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/

tabs

open a file in a new tab
:tabedit [file]
open file browser in new tab
:Texplore
next tab
:gt
previous tab
:gT
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]
close all other split panes
:only
open file browser (split screen)
:Explore
:Sexplore
:Vexplore
open terminal
:terminal
vim cycle split panes
Ctrl-W, direction
ctrl w, w
flip splits
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
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-f    # move left
ctrl w-j    # move down
ctrl w-k    # move up
ctrl w-l    # move right
reorder panes
ctrl w-F    # 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
special registers
*    selection clipboard
+    system clipboard
yank to register
"[register]yy
"[register]Y
cut to register
"[register]dd
paste from register
"[register]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
execute command stored in register "r"
:@r
print filename
:echo @%
:!echo %

macros

record macro in register [a-z]
q[a-z]
[commands]
q
execute macro in register [a-z]
@[a-z]       # run once
[n]@[a-z]    # run [n] times
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

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