Modes
i Insert mode at cursor
I Insert at beginning of line
a Append at cursor
A Append at the end of line
v visual mode
escape Exit insert mode
Basics
:w Save changes to file
:r Read in the same buffer
:q Close file
:q! Close file, don't save changes
:wq or x Save changes and close file
u Undo last change
U Undo changes on entire line
ctrl+r Redo changes
. repeat the previous command
Moving around
escape
j Move cursor down
k Move cursor up
h Move cursor left
l Move cursor right
0 Go to beginning of current line
^ Go to beginning of text on current line
$ Go to end of text on current line
gg move to top of file
G move to the bottom of a file
w Next word
2wGo to words forward
b move over the word backward
e Go to end of word
3e Go to end of third word ahead
f move to the next occurrence of a character
F move to the previous occurrence of a character
n find next occurrence of search_word
N find previous occurrence of search_word
\* find the next occurrence of the word under cursor
# find the previous occurrence of the word under cursor
% Go to matching parenthesis or brackets
CTRL-Y scroll down one line at time
CTRL-E scroll up one line at time
CTRL-D scroll down half page
CTRL-U scroll up half page
Ctrl-F scroll down full page
Ctrl-B scroll up full page
Search
/
/pattern search
:s/pattern/replacement/g search and replace in the current line
:%s/pattern/replacement/gc search and replace in all lines with confirmation
n repeat the search for next occurrences with
N repeat the search for previous occurrences with
Editing
escape
x Delete the character at the cursor
X Delete the character to the left of the cursor
r Replace character under cursor
cw Change word
c2w Change two words
c$ or C Change to end of line
d Delete, it also copies the content, so that you can paste it with p to another location
d$ or D Delete to end of line
dw Delete word
d2w or 2dw Delete two words
dd Delete a line
2dd Delete two lines
p Paste after cursor
P Paste before cursor
o Insert text into a new line below the current one
O Insert text into a new line above the current one
Visual mode
v
e select the word under the cursor
d delete (cut)
y yank (copy)
p put (paste)
Formatting
:retab tab convert
One level of folding, at the cursor:
zc close
zo open
za toggle
All folding levels (for example, the cursor line may be in an open fold, which is inside another open fold; typing zC would close all folds at the cursor) [1]: zC close zO open zA toggle
Auto completion
ctrl-x and ctrl-o start omni completion
Buffer navigation
:ls buffer listing
:b | :b# | :bp & :bn = buffer switching
:e | :badd open a new buffer respectively in foreground and background
:bd close the current buffer
Automatic word wrapping
:set textwidth=n | tw=n left to right margin, set to 0 for reset
:set textwrap=n | tw=n right margin to left, set to 0 for reset
gq re-formating text (in visual mode)
Spell checking
:set nospell | set: spell
:set spelllang=it_it
Automatic writing
:set autowrite
:set noautowrite
File stats
g, ctrl-g show file stats (select text in v mode)
External commands
:! external command (%, run the file)
e.g. :! markdown % | bcat Useful external commands
$ # Preview Markdown with bcat
$ % |markdown |bcat -b chrome
!: git add % && git commit -m 'first commit' && git push origin master
Shifting blocks visually
In normal mode, type >>
to indent the current line, or <<
to unindent
In visual mode press >
to indent (shift text one 'shiftwidth' to the right), or press <
to shift left.
Resources
VIM Adventures
Plugins
pathogen.vim
any plugins you wish to install can be extracted to a subdirectory under
~/.vim/bundle
, and they will be added to the'runtimepath'
.
Emmet-vim Type ("_" is the cursor position):
html:5_
Then type "," (Ctrl + y + ','), you should see:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
_
</body>
</html>