:help keyword
,:h keyword
- open help forkeyword
:saveas file
- savefile
as:close
- close current paneK
- open man page for word under the cursorgf
- open file under cursor (goto file)
h
- move cursor leftj
- move cursor downk
- move cursor upl
- move cursor rightH
- move to top of screenM
- move to middle of screenL
- move to bottom of screenw
- jump forwards to the start of a wordW
- jump forwards to the start of a word (words can contain punctuation)e
- jump forwards to the end of a wordE
- jump forwards to the end of a word (words can contain punctuation)ge
- jump backward to the end of a wordgE
- jump backward to the end of a word (words can contain punctuation)b
- jump backward to the start of a wordB
- jump backward to the start of a word (words can contain punctuation)%
- move to matching character (default supported pairs: '()', '{}', '[]'; use :h matchpairs in vim for more info). It jumps to the one it finds in the current line0
- jump to the start of the line^
- jump to the first non-blank character of the line$
- jump to the end of the lineg_
- jump to the last non-blank character of the linegg
- go to the first line of the documentG
- go to the last line of the document5G
- go to line5
:5
- go to line5
with commandfx
- jump to next occurrence of characterx
in this linetx
- jump to before next occurrence of characterx
in this lineFx
- jump to previous occurence of characterx
in this lineTx
- jump to after previous occurence of characterx
in this line;
- repeat previous f, t, F or T movement,
- repeat previous f, t, F or T movement, backward}
- jump to next paragraph (or function/block, when editing code){
- jump to previous paragraph (or function/block, when editing code)(
- jump to the previous sentence)
- jump to the next sentencezz
- cursor on screen to the centerzt
- cursor on screen to topzb
- cursor on screen to bottomctrl-e
- move screen down one line (without moving cursor), same as+
symbolctrl-y
- move screen up one line (without moving cursor), same as-
symbolctrl-b
- move back one full screen (back full page)ctrl-f
- move forward one full screen (forward full page)ctrl-d
- move forward 1/2 a screen (down half page)ctrl-u
- move back 1/2 a screen (up half page)ctrl-o
- retrace previous cursor positionctrl-i
- retrace next cursor position
Tip: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
i
- insert before the cursorI
- insert at the beginning of the linea
- insert (append) after the cursorA
- insert (append) at the end of the lineo
- append (open) a new line below the current lineO
- append (open) a new line above the current lineea
- insert (append) at the end of the word (so they can be chained with moves)Esc
- exit insert modegi
- go to the last place where insert mode was finishedctrl-r REG
- insert the contents ofREG
register in insert mode.
r
- replace a single characterR
- start replace mode, similar to insert, but overwrites the characters underneathJ
- join line below to the current one with one space in betweengJ
- join line below to the current one without space in betweengwip
- reflow paragraphcc
- change (replace) entire lineC
,c$
- change (replace) to the end of the lineciw
- change (replace) entire wordcw
- change (replace) to the end of the wordc/hello
- change (replace) until next occurrance of "hello"s
- delete character and substitute textxp
- transpose two letters (delete and paste)u
- undo:u
- undo in command modeU
- undo all latest changes on one linectrl-r
- redo:red
- redo in command mode.
- repeat last commandctrl-a
- increase a numberctrl-x
- decrease a number (practice: 4)gu + movement
- makemovement
lowercasegU + movement
- makemovement
uppercaseg~ + movement
- toggle case ofmovement
g+
- undo branch forwardg-
- undo branch backwardea 4h
- undo changes in the last 4 hoursea 2f
- undo last 2 file states (last 2 buffer writes)lat 8m
- redo changes in last 8 minutes
v
- start visual mode. (you can mark text, then do a command (like y-yank))V
- start linewise visual modeo
- move to other end of marked areactrl-v
- start visual block modeO
- move to other corner of blockaw
- mark a wordas
- mark a sentenceap
- mark a paragraphab
- mark a block with ()aB
- mark a block with {}ib
- mark inner block with ()iB
- mark inner block with {}Esc
- exit visual mode
Practice here to select inner block (like this) Or {an inner block} of this.
>
- shift text right<
- shift text lefty
- yank (copy) marked textd
- delete marked text~
- switch case
:reg
- show registers content (can append selectors of which registers to show)"xy
- yank into registerx
"xp
- paste contents of registerx
"Xp
- append contents to registerx
Tip: Registers are being stored in ~/.viminfo, and will be loaded again on next restart of vim. Tip: Register 0 always contains the value of the last yank command.
:marks
- list of marksma
- set current position for marka
`a
- jump to position of marka
'a
- jump to the first non-blank character in the line of marka
y`a
- yank text to position of marka
:delm <pattern>
- delete marks.<pattern>
can be 1 lowercase letter, any number of characters, range of letters or numbers
qa
- record macroa
(it empties that register and appends the keystrokes to it)q
- stop recording macro@a
- run macroa
@@
- rerun last run macro
yy
- yank (copy) a line2yy
- yank (copy)2
linesyw
- yank (copy) the characters of the word from the cursor position to the start of the next wordy$
- yank (copy) to end of linep
- put (paste) the clipboard after cursorP
- put (paste) before cursordd
- delete (cut) a line2dd
- delete (cut)2
linesdiw
- delete (cut) the characters of the whole worddw
- delete (cut) the characters of the word from the cursor position to the start of the next wordD
,d$
- delete (cut) to the end of the linex
- delete (cut) characterviwp
- replace (paste) content of the last used register with the word under the cursor"/p
- paste the last search- `":p" - paste the last command
:w
- write (save) the file, but don't exit:w !sudo tee %
- write out the current file using sudo:wq
,:x
,ZZ
- write (save) and quit:q
- quit (fails if there are unsaved changes):q!
,ZQ
- quit and throw away unsaved changes:wqa
- write (save) and quit on all tabsctrl-z
,:st
,:stop
- suspend vim, start up again withfg
command (optionallyfg %jobnumber
if multiple jobs are selected). Check running suspended jobs withjobs
command
/pattern
- search forpattern
?pattern
- search backward forpattern
/\vpattern
- 'very magic'pattern
: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)n
- repeat search in same directionN
- repeat search in opposite directionggn
- go to first match (assuming forward search)GN
- go to last match (assuming forward search):%s/old/new/g
- replace allold
withnew
throughout file:%s/old/new/gc
- replace allold
withnew
throughout file with confirmations:noh
- remove highlighting of search matches*
- start a search forward with the whole current word under the cursor#
- start a search backward with the current word under the cursorg*
- start a search with the word under the cursor but find occurrences that has more content in it. e.g:rain
findsrainbow
g#
- start a search backward with the word under the cursor but find occurrences that has more content in it. e.g:rain
findsrainbow
:vimgrep /pattern/ {file}
- search for/pattern/
in multiple{file}
s
e.g.:
vimgrep /foo/ **/*
:cn
- jump to the next match:cp
- jump to the previous match:copen
- open a window containing the list of matches
gd
- go to local declarationgD
- go to global declaration
:e
- reload current file:e file
- edit afile
in a new buffer:r file
,:read file
- insert afile
into the current location:0r file
,:0read file
- insert afile
before the first line:r !{cmd}
- execute{cmd}
and insert its standard output below the cursor:bnext
,:bn
- go to the next buffer:bprev
,:bp
- go to the previous buffer:bd
- delete a buffer (close a file):ls
- list all open buffers:sp file
- open afile
in a new buffer and split window:vsp file
- open afile
in a new buffer and vertically split window:sv file
,:sview file
- open afile
in a new buffer, but readonly:vert sv file
- vertically open afile
as readonly as a split
ctrl-ws
- split window horizontallyctrl-wv
- split window verticallyctrl-ww
- switch windows (cycle)ctrl-wq
- quit a windowctrl-wr
- rotate two windows (can not do it if the other one is splitted)ctrl-wh
- move cursor to the left window (vertical split)ctrl-wl
- move cursor to the right window (vertical split)ctrl-wj
- move cursor to the window below (horizontal split)ctrl-wk
- move cursor to the window above (horizontal split)ctrl-w_
- maximize current window verticallyctrl-w|
- maximize current window horizontallyctrl-w=
- make all equal size vertically:res +/-num
,numctrl-w+/-
- horizontally resize by+/-num
of lines or columns:vert res +/-num
,numctrl-w</>
- vertically resize by+/-num
of lines or columns
Tabs should be imagined as layouts. They can show different window arrangements of any buffers.
:tabe file
,:tabnew
,:tabnew file
- open afile
in a new tabctrl-wT
- move the current split window into its own tabgt
,:tabnext
,:tabn
- move to the next tabgT
,:tabprev
,:tabp
- move to the previous tabNUMgt
- move to tab numberNUM
:tabm NUM
,:tabmove NUM
- move current tab to theNUM
th position (indexed from 0):tabc
,:tabclose
- close the current tab and all its windows:tabo
,:tabonly
- close all tabs except for the current one:tabdo command
- run thecommand
on all tabs (e.g.command
= q - closes all opened tabs)
ctrl-n
- in insert mode opens up autocompletectrl-g
- show line infoctrl-o
- in insert mode after this key combo, you can use a command from normal mode, and immediately switch back to the starting mode:term
- open terminal as a horizontal split buffer.
vim file1 file2
- open multiple files (file1
file2
) as buffervim -p file1 file2
- open multiple files (file1
file2
) as tabsvim -o file1 file2
- open multiple files (file1
file2
) as horizontal splitvim -O file1 file2
- open multiple files (file1
file2
) as vertical splitvim file +number
- openfile
at linenumbernumber
:mks workproject.vim
,:mksession workproject.vim
- Your current session of open tabs will be stored in a fileworkproject.vim
vim -S workproject.vim
- load up vim with a session calledworkproject.vim
:source workproject.vim
- load vim session to an opened vim calledworkproject.vim
:mks! workproject.vim
- save changed session tabs while you are in the session calledworkproject.vim
If the filename is omitted then
Session.vim
name will be used
q:
- show prev commands. Close with Ctrl+cq/
- show prev searches. Close with Ctrl+c:
- type in any word and press up. It will look for the prev command that started like that
za
- toggle folding:set foldcolumn=NUM
- visualize folds. showNUM
lines of nested folds per linezfip
- fold the current paragraphzf/string
- fold until next occurrance ofstring
zd
- delete foldzE
- delete all foldszf20j
- fold the next20
lineszf`a
- fold until wherever marka
is in the documentzR
- open all foldszM
- close all folds:mkview
- save folding state:loadview
- load prev folding state
- select text in visual mode then
xi()<esc>P
Wrap brackets around visually selected text - select text in visual mode then
y/ctrl-r"
starts to search for visually selected text :w !diff % -
- show the diffs withdiff
command since last save. (It saves the output to stdin and loads the differences between the current filename and standard input):g/pattern/norm @o
- run the previously recordedo
macro on all lines that matchpattern