Vim cookbook

This is a cheatsheet for vim commands and operations.

Configuration

Display line numbers

• in the current file
:set number # to enable line numbers
:set nonumber # to disable line numbers

• to change config

add set number in ~/.vimrc

set mouse mode on

add set mouse=a to ~/.vimrc

set tab size

add set tabstop=4

Basic movement

• h move to the left by one position

• l move to the right by one position

• j move to the downward direction by one line

• k move to the upward direction by one line

Inline movement

• b move to the start of the current word

• e move to the end of the current word

• w move to the start of the next word

Info: combine movement with a number, e.g. 3w, 9k is the same as pressing w three times, pressing k nine times, respectively.

• ^ move to the first non-blank character

• g_ move to the last non-blank character

• n + Space move to the nth character of the current line

• 0 move to the start column of the current line

• $ move to the end column of the current line Text movement • + move to the first non-blank character of the next line • - move to the first non-blank character of the previous line • :n jump to the nth line • :+n or n + Enter jump down n lines • :-n jump up n lines • gg or :0 move to the first line of the file • G or :$ move to the end line of the file

Info: combine movement with a number, e.g. 3G move to the third line of the file

screen movement

• ctrl + e scroll down a line

• ctrl + y scroll up a line

• ctrl + d scroll down the half of page

• ctrl + u scroll up the half page

• ctrl + f or Page down scroll down the entire page

• ctrl + b or Page up scroll up the entire page

• ctrl + o jump to the previous position

• ctrl + i jump to the next position

Editing

Insert

• i insert the text before the cursor

• I insert the text at the beginning of the line

Open a new line

• o open new line below the cursor

• O open new line above the cursor

Append

• a append the text after the cursor

• A append the text at the end of line

Delete

• x delete the character on the cursor

• X delete the character before the cursor

• dw delete a word beginning at the cursor

• d0 delete from the beginning of current line to the cursor position (including current character)

Others

• :! <command> run the command and show the output outside of vim environment