JiaHe's Blog

读万卷书,行万里路

光标移动

h j k l 上下左右

ctrl-f page up 向上翻页

ctrl-b page down 向下翻页

% jump to matching brace 跳转到匹配的括号

w jump to end of words (punctuation) 跳转到下一个单词,会被标点打断

W jump by words 跳转到下一个单词

e jump to end of words (punctuation) 跳转到下一个单词尾,会被标点打断

E jump to end of words 跳转到下一个单词尾

b jump backward by words (punctuation) 跳转到上一个单词尾,会被标点打断

B jump backward by words 跳转到上一个单词尾

0 start of line 跳转到一行的开头

^ first character of line 跳转到一行的第一个字符

$ end of line 跳转到一行的末尾

gg go to first character of the first line 跳转到第一行的第一个字符

gd go to def of the function or var under the cursor

[N]G go to line N or last line 跳转到N行,或最后一行

f{char} move the cursor forward to the next occurrence of character {char} on the current line 将光标向前移动到下一个x字符的匹配位置,范围是当前行

; repeat last f command 重复上一次f命令

t{char} like f{char}, but move right before the character 类似f{char}命令,但是光标会移动到匹配字符的上一个字符处,如果当前光标本身就停留在匹配字符的上一个字符串,则再执行t{char}时,光标将不会移动。

but move ri[光标]ght [光标2]befo[光标3]re
如果光标在i后面,输入te,则光标会跳转到[光标2]的位置
如果再输入te,光标此时是不会移动的,此时需要将将光标向右移动一个字符,
再输入te后,光标才会移动到[光标3]的位置。

F{char} move the cursor forward to the next occurrence of character {char} on the current line 功能与f{char}类似,不过查找方向相反

T{char} move the cursor backward to the next occurrence of character {char} on the current line 功能与t{char}类似,不过查找方向相反

),( move the cursor to next, previous sentence 移动到下一行或上一行的开头

* read the string under the cursor and go to next occurrence 读取光标下的字符串并跳转到下一个匹配项

# same as * but goes to previous occurrence 与*功能相似,方向相反

`. go to line last edited 跳转到最后一次编辑的位置,我理解的最后一次编辑的位置即按下<Esc>按钮之前的位置。

屏幕移动

H move the cursor to the Highest line on the screen 移动到屏幕靠上的行

M move the cursor to the Middle of the screen 移动到屏幕中间的行

L move the cursor to the Lowest line of the screen 移动到屏幕靠下的行

书签

m{char} make a bookmark name {char} at the current cursor position 在当前光标处创建一个名为{char}的书签

`{char} go to bookmark {char} (backtick, not single quote) 跳转到书签{char}(第一个字符是反引号,不是单引号)

插入模式

i insert mode at cursor 在光标当前位置进入插入模式

I insert at the beginning of line 在光标当前所在行的开头位置进入插入模式

a append after the cursor 在当前光标的后一个字符位置进入插入模式

A append at the end of the line 在当前光标所在行的最后位置进入插入模式

o open blank line below cursor line 在当前光标所在行的下面插入一行,并进入插入模式

O open blank line a bove current line 在当前光标所在行的上面插入一行,并进入插入模式

Esc exit insert mode 退出插入模式

代码补全

ctrl-n 代码补全,向下移动补全项

ctrl-p 代码补全,向上移动补全项

  ab   ctrl-p 选中项移动到这里
a abc 假若当前补全项在这里
abcd ctrl-n 选中项移动到这里

编辑操作

r{char} replace a single character (!insert mode) 替换光标当前所在字符为{char}

J join line below to the current line 删除当前行与下面行之间的一行空白行,如果两行相邻,则将当前行的行尾与下一个行的行头通过空格进行连接

cc change an entire line 删除当前行所有字符,并进入插入模式,此时光标位置在当前行的开头。

cw change to the end of word 删除光标位置到单词结尾的字符,并进入插入模式

c$ change to the end of line 删除光标位置到行结尾的所有字符,并进入插入模式

s delete character at cursor and substitute text 删除光标当前所在字符,并进入插入模式

S delete line at cursor and substitute text 删除光标所在的行,并进入插入模式

xp transpose to letters 将光标所在位置的字符与下一个字符位置替换

u undo 撤销

ctrl-r redo 恢复

. repeat last command 重复最后一次命令

~ switch case 切换当前光标所在字符的大小写

g~iw switch case of current word 切换当前光标所在单词的大小写

gUiw make current word uppercase 将当前单词字母全部大写,如果当前光标位于单词末尾,则不生效。

guiw make current word lowercase 将当前单词字母全部小写,如果当前光标位于单词末尾,则不生效。

make current[光标1] [光标2]word lo[光标3]wercase
光标1位置,不生效
光标2位置,生效
光标3位置,生效

>> indent line one column right 向右缩进

<< indent line one column left 向左缩进

== auto-indent current line 自动缩进当前行

复制 / 粘贴

dd delete (cut) a line 删除当前行

dw delete the current word 删除当前单词

x delete current character 删除当前字符

X delete previous character 删除上一个字符

D delete to end of line 从当前位置删除到行尾

yy yank (copy a line) 复制当前行

nyy yank (copy) n lines 从当前行开始,向下复制n行

yw yank word 复制当前单词,如果单词后面有空格,也会被复制进来

y$ yank to end of line 从当前光标位置复制到行尾所有字符

p put the clipboard after cursor / current line 将剪切板内容粘贴到当前光标所在字符或者当前行的后面

P 与p命令功能相似,方向相反

]p 将剪切板内容放在适当的缩进处

"a use a register named a for the next yank/paste operation 使用名为a的寄存器进行下一次复制粘贴操作

可视化模式

选中文本

v start visual mode 开启面向字符的可视化模式

V start linewise visual mode 开启面向行的可视化模式

ctrl-v start blockwise visual mode 开启面向列块的可视化模式

以上三个命令在已进入可视化模式的情况下,再次使用就会切换到普通模式。

o move to other end of markded area 切换高亮选区的活动端(在进入可视化模式,并已选中文本区域时)

U upper case of marked area 将标记区域的字符转为大写

gv reselect the last highlighted area 重选上次的高亮区域

命令

在进入可视化模式,并且选中文本后,可以执行以下命令来完成相应的操作。

> shift right 右缩进

< shift left 左缩进

y yank 复制当前所选区域

d delete 删除当前所选区域

~ switch case 当前所选区域的大小写

搜索 / 替换

/pattern search for pattern 向下搜索pattern

?pattern search backwards for pattern 向上搜索pattern

n repeat search in same direction 重复上一次的搜索

N repeat search in opposite direction 重复上一次的搜索,但搜索方向相反

:s/{oldpattern}/{newchar}/g replace all old throughout file 使用{newchar}替换整个文件的{oldchar},这里的{oldchar}可以使用正则表达式匹配,{newchar}中可以使用\1来表示引用{oldchar}中第一个用()括号括起来的匹配内容。

:s/{oldpattern}/{newchar}/gc 功能同上,但是替换之前会询问一下。
y 表示替换
n 表示不替换
a 表示替换所有
q 表示退出查找模式
l 表示替换当前位置并退出
^E与^Y是光标移动快捷键

退出

:w save 保存

wq save and quit 保存并退出

x save and quit 保存并退出

q quit, but failed if unsaved 退出,如果有改动但是没有保存的话,会推出失败

q! quit 强制退出,无论有没有保存改动

多文件操作

:e edit a file in a new buffer 在一个新缓冲中编辑文件

:bn go to next buffer 跳转到下一个缓存区

:bd delete a buffer (close file) 删除一个缓冲区(关闭文件)

:sp {file} open a file in new buffer and split window 水平切分当前窗口,并在新窗口中载入{file}

:vs {file} open a file in new buffer and vertical split window 垂直切分当前窗口,并在新窗口中载入{file}

ctrl w - window 命令

ctrl-w s split window 水平分割窗口

ctrl-w w switch windows 切换窗口

ctrl-w q quit a window 退出一个窗口

ctrl-w v split windows vertically 垂直分割窗口

tab 命令

tabe {file} edit file in new tab 在一个新标签页中打开{file}

gt next tab 下一个标签页

gT previous tab 上一个标签页

:tabr first tab 第一个标签页

:tabl last tab 最后一个标签页

:tabm [N] move current tab after tab N 将当前标签页移动到第N个标签页之后

宏命令

q{register} start recording a macro in register {register} 录制宏,并将其内容保存在寄存器a中

q end recording 结束记录

@{register} replay macro in register {register} 执行寄存器{register}中保存的宏

参考:http://jrmiii.com/2009/03/06/learning-vim-the-pragmatic-way.html