Vim
Personal VIM cheatsheet.
Useful Websites
Commands
q/
- opens search history windowq:
- opens command history window:g!/dont/d
- delete all lines not containing the stringdont
g
- is the global commandd
- stands for delete!
- inverts the matching
:g/delete-me/d
- delete all lines containing the stringdelete-me
:!command
- executes an external command.:r
or:read
- read command:r FILENAME
- retrieves disk file FILENAME and puts it below the cursor position.:r !ls
- reads the output of the dir command and puts it below the cursor position.
R
- replace mode. Like insert mode but deletes existing characters- When typing a
:
command, pressCtrl-d
to see possible completions. Press<TAB>
to use one completion. q/
- opens a command window with history of searchesq:
- opens a command window with history of Ex commands2,$!sort -k2
- executesort
cmd in terminal on lines2-end
with-k2
arg to sort by items in second columnedit %:h
- edit active file directory
Copy & move
:copy
or :co
or :t
:move
or :m
:copy $
- copy current line to the end:t6
- copy current line below line 6:6t.
- copy line 6 below current line:m$
- move current line at the end of the file
Normal cmd
:.,$normal A;
- append;
at the end of each line from the current one till the last one
Under the hood
:{start},{end}{command}
.
- current line$
- last line1
- first line0
- zero line (used when you move test to beginning for example:move 0
)%
- all lines in the file<
- start of visual selection>
- end of visual selection
:1,10d
- delete lines 1-10:.,$copy
- copy from current line.
till the end$
Substitute cmds
:s
is just shortcut for :substitute
:s/old/new
- to substitutenew
for the firstold
in a line:s/old/new/g
- to substitutenew
for allold's
on a line:#,#s/old/new/g
- to substitute phrases between two line #'s:%s/old/new/g
- to substitute all occurrences in the file:%s/old/new/gc
- to ask for confirmation each time addc
Project-wide search and replace
/Rg "old" # will populate quick-fix list
:cfdo %s/old/new/gc # will ask you if you want to replace 'old' with 'new' for each occurence in quick-fix list
:wa #write all to disk
Search
/\ccase insensitive search
- use\c
switch/\Ccase sensitive search
- use\C
switchg*
search for word under cursor with partial word matching
Text editing
daw
deletes the whole word wherever the cursor isdip
d
eletei
nsidep
aragraphdas
d
eletea
rounds
entencedi{
d
eletei
nside{
curly bracesvip
v
isual selecti
nsidep
aragraphcip
c
hangei
nsidep
aragraphgU
- change text to UPPERCASEgu
- change text to lowercase
Navigation
Ctrl+o
- backCtrl+i
- forward:jumps
- displays jumps for current window
<number> + G
go to lineCtrl+g
shows info about current position within the file in the bottom lineH
- Highest line on the screenM
- Middle line on the screenL
- Lowest line on the screenzt
- scroll view so that cursor is aligned to thet
opzz
- scroll view so thet cursor is aligned in the middlezb
- scroll view so that cursor is aligned to theb
ottom
Automatic Marks
"
position before the last jump within current file'.
location of last change'^
location of last insertion'[
start of last change or yank']
end of last change or yank'<
start of last visual selection'>
end of last visual selection
Tabs
gt
(or:tabn
) - to go to next tabgT
(or:tabp
or:tabN
) - to go to previous tab#gt
(or:tabn #
) - to go to #th tab
Motions
g;
- go to most recent modification in the document- press again and you go to most recent - 1
`.
go to most recent modification`^
go to last insertion0
go to the start of the line^
go to the first character on the line
Spell checking
:setlocal spell
enable spell checking:set nospell
disable spell check]s
move to next misspelled word[s
move to previous misspeled wordzg
mark word as a good wordzG
mark good just for current instance (will be lost after restart)zw
mark word as wrong wordzW
mark word as wrong word for current instancez=
suggest correction
Netrw explorer
-
go one level up in direcotry tree:Vex
vertical split:Sex
horizontal split%
create new fileR
rename fileD
delete filed
create new diri
change view
Registers (a.k.a. clipboard)
"{register-name}{y|p|d}
"byy
copy tob
register"bp
paster fromb
register"bdd
delete tob
registero
register holds what wasyank
ed most recently- lowercase letter overwrites register, uppercase appends to it
"_
black hole register - a place from which nothing returns*
register references system clipboard ("*p
,"*y
)
Buffers
:buffers
or:ls
to list all open buffers:ball
opens all buffers in windows:bd
close current buffer (buffer delete):bnext
or:bn
:bprev
or:bp
%bd
delete all unsaved buffers (it will refuse to delete unsaved ones)
Windows
Ctrl+w =
resize windows equallyCtrl+w 10 >
change width to left by 10Ctrl+w 10 <
change width to right by 10Ctrl+w 10 +
incerease height by 10Ctrl+w 10 -
decrease height by 10:res[ize] +10
increase window height/width by 10:res[ize] -10
decrease window height/width by 10z10
set height/width to 10:only[!]
close all other windows except this one (add ! to close even unsaved ones)
Splits
Ctrl+w
followed by capitalH
,J
,K
orL
will move the current window to the far left, bottom, top or right respectively like normal cursor navigationCtrl+w
followed byShift+T
will move split to new tab
Insert mode
Ctrl+w
delete back one wordCtrl+u
delete all entered characters before the cursorCtrl+h
delete one character (backspace)Ctrl+o
enters Insert Normal Mode -> You can execute one command (e.g.:zz
) and then return back to Insert mode
Visual mode
Ctrl+v
block-wise visual modegv
reselect the last visual selectiono
go to the other end of visual selection
Folding
Collapsing multiple lines of text into single line
zf{motion}
- fold (e.g.:zf{
folds inside{}
brackets)za
- toggle foldzR
- open all folds in current bufferzM
- close all folds in current bufferzc
- close foldzo
- open foldzd
- delete fold under cursor (not text, just fold)
Macros
q{a-z}
to record@{a-z}
to replay@@
to replay the most recently replayed macro
External commands
:%!<command>
- runs external command with current buffer content:%!sort
will runsort
with current buffer