This article contains some useful skills in Vi/Vim.

Basic Commands

Move

Basic

key function
h, left go left one character
j, down go down one line
k, up go up one line
l, right go right one character
e, E go to end of word, WORD
w, W go to begin of next word, WORD
b, B go to previous word, WORD

Go to line

  1. :
  2. 123 type the number of line you want to go
  3. Enter to go

Search words

  1. /
  2. word type the “word” you want to find
  3. Enter to start searching
  4. n / N to search next/prev

Search current word

  1. move cursor to the target word
  2. # / * to find prev/next

Modification

command function
yy copy current line
yw copy current word from cursor
p paste after cursor/line
u undo
dd delete current line
x delete current character

Enter insert mode

key function
i enter insert mode before the cursor
I enter insert mode at the start of line
a enter insert mode after the cursor
A enter insert mode at the end of line
o enter insert mode at a new line up
O enter insert mode at a new line down

File operation

  1. :
  2. command type the command
  3. Enter to go
command function
w save
q quit
wq save & quit
q! don’t save & quit
e refresh file
e! discard change and refresh file

Some Skills

Comment / Uncomment

Comment

  1. Ctrl + v enter block visual mode
  2. j/k to select lines
  3. I to insert at the beginning
  4. // to add characters
  5. Esc to finish

Uncomment

  1. Ctrl + v enter block visual mode
  2. j/k/h/l to select characters
  3. x to delete

Autofill

Ctrl + p

Replace

Replace all xx to yy in this file. Confirmation required. :%s/xx/yy/gc

Replace xx to yy between line 10 to 100. Confirmation not required. :10:100s/xx/yy/g

Reference

vim cheatsheet