Vim Cheat Sheet
A Great Vim Cheat Sheet
Essentials
Cursor movement (Normal/Visual Mode)
h
j
k
l
- Arrow keysw
/b
- Next/previous wordW
/B
- Next/previous word (space seperated)e
/ge
- Next/previous end of word0
/$
- Start/End of line^
- First non-blank character of line (same as0w
)
Editing text
i
/a
- Start insert mode at/after cursorI
/A
- Start insert mode at the beginning/end of the lineo
/O
- Add blank line below/above current lineEsc
orCtrl+[
- Exit insert moded
- Deletedd
- Delete linec
- Delete, then start insert modecc
- Delete line, then start insert mode
Operators
Operators also work in Visual Mode
d
- Deletes from the cursor to the movement locationc
- Deletes from the cursor to the movement location, then starts insert modey
- Copy from the cursor to the movement location>
- Indent one level<
- Unindent one level
You can also combine operators with motions. Ex: d$
deletes from the cursor to the end of the line.
Marking text (visual mode)
v
- Start visual modeV
- Start linewise visual modeCtrl+v
- Start visual block modeEsc
orCtrl+[
- Exit visual mode
Clipboard
yy
- Yank (copy) a linep
- Paste after cursorP
- Paste before cursordd
- Delete (cut) a linex
- Delete (cut) current characterX
- Delete (cut) previous characterd
/c
- By default, these copy the deleted text
Exiting
:w
- Write (save) the file, but don’t quit:wq
- Write (save) and quit:q
- Quit (fails if anything has changed):q!
- Quit and throw away changes:qa
- Quit all windows:wqa
- Write and quit all windows
Search/Replace
/pattern
- Search for pattern?pattern
- Search backward for patternn
- Repeat search in same directionN
- Repeat search in opposite direction:%s/old/new/g
- Replace all old with new throughout file (gn is better though):%s/old/new/gc
- Replace all old with new throughout file with confirmations
General
u
- UndoCtrl+r
- Redo
Advanced
Cursor movement
Ctrl+d
- Move down half a pageCtrl+u
- Move up half a page}
- Go forward by paragraph (the next blank line){
- Go backward by paragraph (the next blank line)gg
- Go to the top of the pageG
- Go the bottom of the page: [num] [enter]
- Go to that line in the documentnumber + j / k
- Go to line relative line number, down or upctrl+e / ctrl+y
- Scroll down/up one line
Character search
f [char]
- Move forward to the given charF [char]
- Move backward to the given chart [char]
- Move forward to before the given charT [char]
- Move backward to before the given char;
/,
- Repeat search forwards/backwards
Editing text
J
- Join line below to the current oner [char]
- Replace a single character with the specified char (does not use Insert mode)
Visual mode
O
- Move to other corner of blocko
- Move to other end of marked area
File Tabs
:e filename
- Edit a file:tabe
- Make a new tabgt
- Go to the next tabgT
- Go to the previous tab:vsp
- Vertically split windowsctrl+ws
- Split windows horizontallyctrl+wv
- Split windows verticallyctrl+ww
- Switch between windowsctrl+wq
- Quit a window:vs|:te
or:vs+te
- Vertical split with new term:sp+te
- Horizontal split with term
Marks
- Marks allow you to jump to designated points in your code.
m{a-z}
- Set mark {a-z} at cursor position
- A capital mark {A-Z} sets a global mark and will work between files
'{a-z}
- Move the cursor to the start of the line where the mark was set''
- Go back to the previous jump location
Text Objects
Say you have def (arg1, arg2, arg3)
, where your cursor is somewhere in the middle of the parenthesis.
di(
deletes everything between the parenthesis. That says “change everything inside the nearest parenthesis”. Without text objects, you would need to doT(dt)
.- Learn more
General
.
- Repeat last commandCtrl+r + 0
in insert mode inserts the last yanked text (or in command mode)gv
- reselect (select last selected block of text, from visual mode)%
- jumps between matching()
or{}
My tweeks:
" Alt+,/.
nnoremap <M-,> :tabprevious<CR>
nnoremap <M-.> :tabnext<CR>
" shift+alt+j/k | duplicate selectec line down/up
nnoremap <S-M-j> :t.<CR>
nnoremap <S-M-k> :t-<CR>
" alt+j/k move selection down/up
nnoremap <A-j> :m .+1<CR>
nnoremap <A-k> :m .-2<CR>