Vim cheatsheet
Made an tested with an azerty keyboard; paths relate to Debian.
In case of emergency
- Quit Vim:
:q - Quit Vim anyway:
:q! - Help:
F1(quit help::q) - Help about command:
:help command - If the requested command name is incomplete, use
TABfor auto completion ofCTRL+dto get the list of available commands
Vim modes
- Normal mode:
ESC - Insert mode, before or after cursor:
iora- Replace mode:
R
- Replace mode:
- Visual mode:
v(“cursor visual”) orV(“line visual”) - Command mode:
:
Navigate
Navigate in a file
- Move cursor: left, down, up, right:
h,j,k,l, respectively; can be preeceded by a number. - Go to the start or end of line:
0or$ - Go to start or end of block:
ALT+{orALT+} - Go to the start or end of file:
ggorG - Go to line number n:
nGG - Show position status:
CTRL+g
Navigate through words
- Go to next word:
w - Go to end of word:
e - Go to previous word:
b - Go to beginning of current word:
b - Go to next or previous pattern under cursor:
*or# - Go to matching bracket when on
(,),[,],{,},<, or>:%
Apart from %, these commands can be preceeded by a number; e.g. 5 w goes 5 words forward.
Search
- Search for a word:
/word - Backward search:
?word - Got to next or previous match:
norN - Highlight search result:
:set hls - Ignore case:
:set ic
Selection
- Enter in cursor visual visual mode:
v - Enter line visual visual mode:
V - When in visual mode, most of the navigation commands above will select the relevant text; e.g.
vGwill select text from cursor to the end of the file. - Save selection to filename:
:w filename - Copy selected text:
y - Delete selected text:
d - Paste the copied selection before or after cursor or line:
Porp
Edition
- Copy:
y - Paste:
Porp - Copy and paste require some text to be selected; see section “Selection” above.
- Undo:
u - Redo:
CTRL+r
Indentation
- Indent current line:
>> - Remove current line indentation:
<< - Indent selection:
>;.to repeat - Remove selection indentation:
<; repeat with. - Example:
V}>.selects the current line up to the end of the block and indents it 2 times
Comments
TODO
Deletion
- Delete char under cursor:
x - Delete current line:
dd(orVd) - Delete n lines:
ndd - In addition,
dcan be combined to most of the navigation commands above, e.g.d0will delete text from cursor position to the beginning of the line;
Replacement
- Replace char under cursor with a:
ra - Replace mode:
Rand overwrite;ESCwhen finished
See also substitution commands below.
Insert mode
- Enter in insert mode before or after cursor:
iora - Go to beginning or end of line and enter visual mode:
IorA - Delete text and enter insert mode:
ccombined with a displacement; e.g.c2wdeletes 2 words and enters to insert mode - Delete until end of line and enter insert mode:
C - Open a new line before or after current line and enter insert mode:
Ooro
Substitution
- Replace a by b:
:s/a/b - Replace a by b in the wole line:
:s/a/b/g - Replace a by b in the whole document:
:%s/a/b/g - Replace a by b between line n°x and line n°y:
:x,ys/a/b/g - Ask for confirmation before replacement: use
gcinstead ofg
I/O
- Save file:
:w - Save and quit:
:wqor:x - Save to filename:
:w filename - Read from filename and put data after current line:
:r filename - Execute a shell command:
:!command - Execute command and set output after current line:
:r!command
Layout
Vertical pane
- Vertical split:
CTRL+w v - Increase width of vertical pane:
CTRL+w > - Increase width of vertical pane by n:
n CTRL+w > - Decrease width of vertical pane:
CTRL+w < - Decrease width of vertical pane by n:
n CTRL+w < - Set width=n:
n CTRL+w |
Horizontal pane
- Horizontal split:
CTRL+w s - Increase height of horizontal pane:
CTRL+w + - Increase height of horizontal pane by n:
n CTRL+w + - Decrease height of horizontal pane:
CTRL+w - - Decrease height of horizontal pane by n:
n CTRL+w - - Set height=n:
n CTRL+w _
Common
- Equalize 2 panes:
CTRL+w = - Navigate between panes:
CTRL+w hjkl - Close pane:
CTRL+w c - Open the file explorer to the left:
:Lexplore ENTER
Color
- Show current color scheme used:
:colorscheme - List available color schemes:
:colorscheme TABor:colorscheme CTRL+d - Use colorscheme name:
:colorscheme name
Default methods for highlighting are found in file /usr/share/vim/vim<version>/syntax/syncolor.vim; I edit this file when I don’t want things like special words being highlighted in bold.
Additional keywords can be highlighted for a given language, e.g. C:
1
2
3
4
5
6
7
cat ~/.vim/after/syntax/c.vim
syn keyword msType QWORD DWORD WORD BYTE
syn keyword msType BOOL HANDLE PSLIST_HEADER HWND ATOM HBRUSH WNDCLASSA
syn keyword msConstant TRUE FALSE WM_CLOSE WM_DESTROY WM_TIMER
hi link msType cType
hi link msConstant cConstant
New color schemes can be installed. I put the one I’m using in /usr/share/vim/vim<version>/colors/.
EOF
This post is licensed under CC BY 4.0 by the author.