Skip to content
Austin Kong edited this page Sep 9, 2020 · 4 revisions

Vim cheatsheet

.   # repeat last command/block of commands (eg insertion of text)
u   # undo
U   # redo
^R  # redo (undo the undo)
:!  # execute external commands

Basic cursor motion

       ^
       k             Hint:  The h key is at the left and moves left.
< h       l >               The l key is at the right and moves right.
     j                      The j key looks like a down arrow.
     v

0   # start of line
^   # first non blank char
$   # end of line

w   # start of next word (excludes)
b   # start of previous word
e   # end of current word (includes)

gg  # first line
G   # last line

^G  # show line and col number
5G  # 5th line

H   # screen top
L   # screen bottom

f)  # jump to next occurance of ')', use ',' ';' to go to prev/next one
F   # jump to previous occurance of ...
t)  # jump to before next occurance of ')' on the same line 
T   # jump to before previous occurance of ...

%   # select matching bracket, or jump to nearest if enclosed

Selections

v   # visual select
V   # visual select whole line
^V  # visual block (column editing) use I to insert before in block mode

gv  # reselect lines

Motion and editing

i   # insert before cursor (use open bracket/quote as motions to select enclosed region for editing)
I   # insert at start of line
A   # insert at end of line
O   # insert on previous line
o   # insert on next line

x   # delete character under cursor
X   # delete character before cursor
d   # delete motion (use open bracket/quote as motions to select enclosed region for editing)
dd  # delete/cut line
D   # delete to end of line (leaves newline)

y   # yank/copy
yy  # yank/copy line
Y   # yank/copy to end of line (excludes newline) ????
p   # paste after cursor
P   # paste before cursor

cw  # delete to the end of current word and start inserting in its place (any movement command can be substituted for w)
cc  # delete line and start inserting
S   # substitute line, as above
C   # delete from cursor to end of line and start inserting
dt) # delete till ')' (excluding), same for t
df) # delete till ')' (including), same for t

~   # toggle case
gU  # make uppercase
xp  # transpose char

=   # indent
<   # shift left
>>  # indent line, prepend with operator
<<  # unindent line
^T  # indent in visual
^D  # unindent in visual
==  # autoindent line

Search and replace

/   # search forward, n for next, N for previous
?   # search backwards
*   # searches word under cursor
#   # searches backwards for word under cursor

:s/foo/bar/g       # Change each 'foo' to 'bar' in the current line.
:%s/foo/bar/g      # Change each 'foo' to 'bar' in all the lines.
:s/foo/bar/gic     # per line, all occurances, case insenstive, ask for confirmation
:%s/foo/bar/gI     # whole doc, case sensitive
:%s/\<foo\>/bar/gc # Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.
:5,12s/foo/bar/g   # Change each 'foo' to 'bar' for all lines from line 5 to line 12 (inclusive).

Splitting screen

:sp      # split window hori, add file name after to open as well
:vs      # split window vert

^w hjkl  # move cursor to window l/d/u/r, also works with arrows
^w ^w    # move cursor to next window
^w t     # move cursor to top left window

^w HJKL  # move current window to l/d/u/r and make full width
^w r     # rotate windows CW

^w +-<>  # adjust window height/width, prepend with motion
^w vS    # split left-right/up-down
^w _=    # maximise current window/make windows equal size
^w q     # closes split
^o       # jump back
^i       # jump forward

File manipulation

:r       # read external output into current file
:e       # open for editing
:w       # save file (add filename to save as)
:wa      # save file all open files
:wq      # save and quit
:xa      # save all and quit
:qa!     # force quit all

ZZ       # write and quit
ZQ       # quit without saving

:ls      # list open files
:bn      # next file in buffer
:bp      # prev file in buffer
:bd      # delete file from buffer
:enew    # create new file or :new ?

Custom syntax highlighting

Put the syntax file at .vim/syntax/foo.vim

Create a file at .vim/ftdetect/foo.vim and populate with:

autocmd BufNewFile,BufRead *.foo,*.foo1 set filetype=foo

Misc

Enable the use of numpad via PuTTY Terminal > Features > Disable application keypad mode

[Vim sensible] (https://github.com/tpope/vim-sensible)