All Notes

My vim cheat sheet

~/.vimrc settings file
vim filename open or create file if not exists
vim +n filename open file at line n
vim + filename open file at last line
vim <command> filename run command after opening file

Basics

i, INSERT       # insert mode
ESC             # view mode
v               # visual selection mode

Cursor movement

hjkl            # move cursor (please don't use arrow keys!)
w               # move forward by word
b               # move backward by word
e               # move to end of word
w               # go to next word
0               # move to beginning of current line
^               # move to first non blank character of current line
$               # move to last character of current line
+               # move to first character of next line
-               # move to first character of previous line
H               # move to top line on screen
M               # move to middle line on screen
gg              # go to beginning of file
G               # go to end of file
<n>|            # go to column n of current line
<n>G            # go to line n
f<c>            # go to next character c in the current line
Ctrl-u          # scroll up half screen
Ctrl-d          # scroll down half screen
Ctrl-f          # scroll up one screen
Ctrl-b          # scroll down one screen
z Enter         # move current line to top of screen
:/<regexp>      # search pattern, n(N) find next(perv) occurrence
                # / Enter - repeat search forward
                # ? Enter - repeat search backward
:g/<regexp>     # search last occurence of pattern
:g!/<regexp>/nu # display all matched lines and their numbers
:?<regexp>      # search backward for pattern
mx              # make named mark x
'x              # move cursor to line marked x
`x              # move cursor to character marked x

Editing

i               # insert mode at current position
I               # move to beginning of line + insert mode
a               # append text at current position
A               # append text to end of current line
u               # undo
U               # restore line to original state
Ctrl-r          # redo
o               # insert blank line below cursor + insert mode
O               # insert blank line above cursor + insert mode
yy              # yank (copy) line to the buffer
yw              # yank word
p               # put text from the buffer after the cursor
P               # put text from the buffer before the cursor
dd              # delete line
dw              # delete word starting from current cursor position
db              # delete word backward
d^              # delete to the beginning of line
D, d$           # delete to the end of line
cc              # replace current line + insert mode
x               # delete character under the cursor
s               # delete character under the cursor + insert mode
S               # delete line + insert mode
R               # replace (overstrike existing text)
:/<regexp>/d    # delete matched line
:s/old/new/     # replace
:s/old/new/g    # replace all
xp              # transpose characters
J               # join lines
~               # toggle case

Other

Ctrl-g          # show file info
Ctrl-l          # redraw the screen
"a7yy           # yank next 7 lines into buffer a
"ap             # put buffer a after cursor
.               # repeat last command
:split [file]   # split horizontally(`Ctrl-w hjklw` to move around)
:vsplit [file]  # split vertically

Macro

Very useful and powerful!

q{register}     # start macro recornig and do some
                # sequence of commands
q               # stop macro recornig
@{register}     # run macro, could be repeated with . (dot)

Commands

Almost all set command commands may be turned off with set nocommand.

:set all        # display all options and defaults
:set option?    # display option value
:w              # write changes
:w filename     # write changes to filename
:wq             # save and exit
ZZ              # save and exit
:q              # quit
:q!             # quit without save
:reg            # show all registers
:set nu         # display line numbers
:set ic         # ignore case (appled to search/replace patterns)
:=              # print total number of lines
:.=             # print current line number
:Ex             # file navigator
:!<command>     # run unix command, example :!ls
:sh             # run shell session, press Ctrl-d to exit