This document aims to highlight useful standard vim commands.
Useful link: https://devhints.io/vim
The main usage of vim is EDITING files. This section details ways to navigate around text objects windows and documents and basic manipulation.
b
- Previous Wordw
- Next Wordge
- End of Previous Worde
- End of Next Word0
- Start of Line^
- Start of Line (after whitespace)$
- End of Linefx
- Go forward to Character xFx
- Go previous to Character x
gg
- First LineG
- Last Line:4
- Go to Line 4zz
- Centre the Window at this linezt
- Move this Window with this line at the topzb
- Move this Window with this line at the bottomH
- Move to top opf screenM
- Move to middle of screenL
- Move to bottom of screen<C-b>
- Move back one full screen<C-f>
- Move forward one full screen<C-d>
- Move forward 1/2 screen<C-u>
- Move back (up) 1/2 screen
%
- Nearest/matching{[()]}
[(
,[{
,[<
- Previous bracket])
,]}
,]>
- Next bracket[m
- Previous method start[M
- Next method start
<C-O>
- Go to previous jump<C-I>
Go to next jumpgf
- Go to file in cursor;
- repeat movement forward,
- repeqte movement backwards
Reference: Using Marks
mx
- Set mark, where *x * can be any character\
x` - Go to mark, where *x * can be any character\
]'` - jump to next line with a lowercase mark['
- jump to previous line with a lowercase mark- `]`` - jump to next lowercase mark
- `[`` - jump to previous lowercase mark
:delmarks x
- Delete Mark
Objects can be modified by 3 keys – the first being the operator (such as c for change), the second being the method (such as i for inner), and the last being the text object (such as w for word). So caw
would be change around word.
c
- Changed
- Deletev
- Visualy
- Yank
a
- Around; include whitespace/newlinei
- Inner; inside object only
p
- Paragraphw
- Words
- Sentence[
,(
,{
,<
A - [], (), or {} block'
,"
- A quoted stringt
A - XML tag block
a
- AppendA
- Append from end of linei
- Inserto
- Insert above lineO
- Insert below lines
- Delete char and insertS
- Delete line and insertC
Delete until end of line and insertr
- Replace one characterR
- Enter Replace modeu
- Undo Changes<C-R
- Redo changes
- Use
Ctrl+V
to enter visual block mode - Move Up/Downto select the columns of text in the lines you want to comment.
- Then either:
<S-i>
- Insertc
- Change
- Then hit
Esc
, wait 1 second and the inserted text will appear on every line.
/{REGEX}
ctrl-g
- go to next foundctrl-t
- go to previousctrl-p
- previous search (history-1)ctrl-n
- next search (history+1)
zo
- Open fold /zO
Open fold recursivelyzc
- Close fold /zC
Close fold recursivelyza
- Toggle foldzM
- Close all foldszR
- Open all folds
On top of editing singular files we can use Vim to organise files in tabs. We can create new files (via buffers) and
File navigation is done with netrw
: :Explore
, :Sexplore
,:Vexplore
, Texplore
.
And within netrw, we'll want to manipulate the files:
<C-r>
- Open directory/file<del>
- Delete file<C-l>
- Refresh directoryd
- CREATE a directory%
- CREATE a new fileD
- DELETE a file/firectoryp
- PREVIEW a fileR
- RENAME a file or directoryo
- OPEN file/directory in HORIZONTAL splitv
- OPEN file/direxctory in VERTICAL splitx
- OPEN file in system file editor (useful for binary files, for example)gn
- CHANGE root directory-
- CHANGE directory to parent
See https://gist.github.com/danidiaz/37a69305e2ed3319bfff9631175c5d0f for an exhaustive list
mt
- mark destination directorymf
- mark filemc
- perform copy
<C-o>
- Go to previous buffer (jump)<C-i>
- Go to next buffer (jump) if set:tabnew
- New buffer in tab:new
- New buffer in horizontal split:Vnew
- New buffer in vertical split
Mode-specific usage
:%s/A/B/g
- sub whole file:s/A/B/g
- sub:&
- Repeats last substitution but resets the flags. Also works with just :s.:&&
- Repeat last substitution with the same flags.:%&
- Repeat last substitution on entire file, reset flags.:%&&
- Repeat last substitution on entire file, keep flags.
gv
- Go to previous visual block (via normal mode)<C-<>
- Indent Left<C->>
- Indent Right<C-=>
- Auto Indent
<C-w>=
- equal size<C-w>>
- increase v size<C-w><
- decrease v size<C-w>+
- increase h size<C-w>-
- decrease h size<C-w>K
or<C-w> J
- move splits left/right<C-w><Bar>
- enlarge current pane
gt
- Go to next tabgT
- Go to previous tab3gt
- Go the third tab
<S-k>
- Jump to help for vim symbol
Diff two files with vim
]c :
- next difference[c :
- previous differencedo
- diff obtaindp
- diff putzo
- open folded textzc
- close folded text:diffupdate
- re-scan the files for differences