Vim cheat sheet

https://vim.rtorr.com/

Use relative line mode with vim

In vim, you will frequently need to manipulate multi line with 3dd 4yy or something else, relative line mode in IDE could help you easy to count the line number.
if you are using VScode like me, you could enable it by add the line to user setting:

1
"editor.lineNumbers": "relative",

Get difference before save

1
:w !diff % -

Explanation

  • w without filename will save to stdin
  • ! will excute bash in vim
  • % is current file in vim
  • diff with - will read content from stdin

Useful vim command combination list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Make a word uppercase
gUe

# edit neariest word inside symbol, e.g. }
ci}

# edit neariest word outside symbol, e.g. ]
ca]

# edit and delete from cursor to next symbol, e.g. )
ct)

# edit and delete from cursor to previos symbol, e.g. )
cT)


# replace word with yanked word
viwp

Common command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# get recent command list
q:

# Replace word A with word B
:%s/A/B/g

# Next word
w

# Previous word
b

# copy (yank)
y

# copy line
yy

# paste
p

# delete
d

# Undo
u