:version to check version, features and settings

0 移动与定位

h j k l

H M L
move cursor to top mid bottom

CTRL-Y / CTRL-E ; CTRL-U / CTRL-D ; CTRL-B / CTRL-F
scroll up/down by line; half page; page

w b word

e end of a word

0 $ start and end of current line

gg G to move to the start(bottom) of the file

11gg or 11G to move to line 11

CTRL-G show location and status

zz zt 移动当前行至页中,页首,前面可以加行数,如 99zt

/ or ? search FORWARD or BACKWARD command

/fuck search fuck

?fuck search fuck in the backward direction 向前搜索

n N next(previous) fuck

CTRL-O jump back to where you came from

CTRL-I jump forward

f[key] or F[key] search the [key] FORWARD or BACKWARD, and ; go to the next
t[key] or T[key] search till the [key] FORWARD or BACKWARD

% move to the matching parenthesis or bracket (,) , [,] , {,}

%( find the matching )

NOTE: This is very useful in debugging a program with unmatched parentheses!

* move to the next match of the word under the cursor

used with n and N

. redo the command, repeats the last change

used with n
用于搜索后对同一词的批量操作

跳转

[(, [{, jump to the first ( or { before, used with %
]), ]}, jump to the first ) or } after
gd will take you to the local declaration.
gD will take you to the global declaration.
g search for the word under the cursor (like , but g on 'rain' will find words like 'rainbow').
g# same as g* but in backward direction.
gg goes to the first line in the buffer (or provide a count before the command for a specific line).
G goes to the last line (or provide a count before the command for a specific line).
gf will go to the file under the cursor
g] and other commands will jump to a tag definition (a tag can be a function or variable name, or more).

tags

CTRL-]
:tn
:tp

1 编辑

插入

i I insert

a A append

o O open command, open a new line below(above) the cursor

:r retreiving and merging files

:r FILE insert the contents of the FILE

:r ! external command read the output of an external command

:r !ls

选择

v

viw "visual mode, inner word",选择光标所在单词

vi{ 同理,选择{}内的内容
va( 选择()以及()内的内容
vip paragraph

V 选择一行

Ctrl+v 块选择

删除

x 删除当前字符, 4x

d delete

dd 删除当前行, d3d3dd

dw delete to the start of the next word, d2w

de delete to the end of the current word

d0 d$ delete to the start(end) of the line

D delete from cursor to the end of the line

c change

ce change until the end of a word

cb

cw 删除当前字符至单词结尾,并进入insert模式 c2w

c$

ciw caw "change inner/a word",同上,光标在单词上即可,完成后进入insert模式,i或a

ci{ "change inner { block"

ca( 改变包括()在内

cc change the line, same with S

s delete and insert

S change the line, same with cc

替换

r replace

R replace more than one character

:s substitute command

:s/thee/the to substitute 'the' for 'thee', only changes the first occurrence of "thee" in the line

:s/thee/the/g to substitute globally in the line

:#,#s/old/new/g where #,# are the line numbers of the range of lines where the substitution is to be done. :5,8s/fu/fuck/g

:%s/old/new/g to change every occurrence in the whole file.

:%s/old/new/gc to find every occurrence in the whole file, with a prompt whether to substitute or not.

复制粘贴

y yank(copy)

yy copy a line, nyy, copy n(a number) line

用命令 v 选择文本后 y

yw yank one word

Du 复制至末尾,不含换行
0Du0v$hy0vg_y复制一行,不含换行

p put(paste)

viwp "viual mode, select inner word, paste"把当前单词换为剪贴板里的内容, vi{p 同理

撤销

u undo

U fix a whole line

CTRL-R redo

2 文件

打开

在终端里输入 vim file1 file2 file3

:n file

:open file

:sp 直接打开并按 split 排列

:vsp 直接打开并按 vsplit 排列

保存

:w

可在用 v 命令选择文本后将所选文字用 :w file 另存为 file,部分保存

插入文件

:r filename
:r !command
:r !date插入当前时间

Tab

vim -p file1 file2 file3

:tabe file:tabnew open a new tab page and edit file like :edit

:tabngt tab next

:tabpgT tab previous

:tabc close tab

显示

:split

:vsplit

切换

文件间切换:

下一个: Ctrl+6:bn,上一个 :bp

窗口间切换:

Ctrl+w+h/j/k/l ,下一个: Ctrl+ww

跳转至光标下文件

gf
gf in new tab

nnoremap gf <C-W>gf

3 其它操作

:set set options

:set xxx

some options are:

'ic' 'ignorecase' ignore upper/lower case when searching

'is' 'incsearch' show partial matches for a search phrase

'hls' 'hlsearch' highlight all matching phrases

:set noxxx to disable set options, :set nohls

:help or F1 to get help

:help w

:help user-manual

:help vimrc-intro

TAB or CTRL-D command line completion 补全

CTRL-G to show location in the file and the file status
select in visual mode, g + CTRL-G count line

把长行截短

:set tw=80设置textwidth为80
gqqgq命令为格式化行,后跟移动,gqq为当前行

多行执行同一编辑

:[line1],[line2] norm [edit_command]
如:把15-34行都用""括起来:
:15,34 norm I"
:15,34 norm A"
例:给每一行后添加:80
:% norm A:80 或者 :%s/$/:80

% 意思是全文,所有行
$ 代表行尾

macro, from a to z

With recording you can repeat many different actions, not just inserting text.
Keep this is mind when you know you are going to repeat something.

qa 开始记录macro
q 结束记录
@a 应用a,可以100@a,执行100次

Ctrl+a

increment the number under cursor
Ctrl-v select, then g Ctrl-a increment all

J & gJ

J combine lines, with space
gJ no space

4 外部命令

:! followed by an external command to executed that command

5 特殊字符

:digraphs to show
CTRL-K in the insert mode to insert