前言
随着开发的深入,使用 vim 的频率渐次提升,但是技巧却始终停留在初见阶段(一些技巧虽然学过但是都忘了。。。),极大地影响了开发的效率,所以,再次学习 vim 势在必行。
1. 基础
| Arguments | Description |
|---|---|
| +[num] | Open editor with cursor on line “num”. If “num” is not specified, the cursor will be on the last line of the file. |
| +/{pat} | Open editor with cursor on the first occurrence of {pat}. |
| -c {command} –cmd {command} | A “ex” command in dowble quotes will be processed against the file specified. |
| -b | Binary file mode. |
| -C -v | VI compatibility mode. Loses the more advanced vim features. |
| -d | Diff file mode. Must list all files to perform a diff upon (list 2, 3 or 4 files). Same as vimdiff. |
| -g | GUI gvim mode (if compiled in and available). |
| -h –help | Print help messages. Also see vimtutor |
| -i filename | Specify viminfo file. Default is ~/.viminfo |
| -r -L | Recovery mode. Used after a crash. The “.swp” file is used. See “:help recovery”. |
| -M -R | File modifications and write not allowed. |
| -n | Prohibit “.swp” file generation. Required for special devices of limited space. |
| -x | Use encryption when writing files. Will prompt for a crypt key. |
| –noplugin | Skip loading plugins. |
| –version | Print vim version. |
2. 常用命令的复习与强化
2.1 移动
单词级
worWMove cursor a word at a timeeorEMove cursor to end of wordborBMove cursor back a word at a time
对于中文和英文,区分单词的界限不同
块级
gg到文档第一行G到文档最后一行0到行首(第 1 列)^到第一个非空白字符$到行尾H移动到屏幕顶端M移动到屏幕中间L移动到屏幕底部Ctrl-d向下移动半页Ctrl-u向上移动半页Ctrl-f向下移动一页Ctrl-b向上移动一页:<N>or<N>gg跳转到第 N 行:+<N>or<N>j向下跳 N 行:-<N>or<N>k向上跳 N 行
2.2 查找
*向后查找光标当前所在单词#向前查找光标当前所在单词/<search>向后查找指定字符串?<search>向前查找指定字符串n继续查找下一个N继续查找上一个
注意: n 和 N 是有方向性的,若你之前通过 * 查找,则 n 会继续向文档尾方向查找,N
向文档首方向;反之,若你通过 # 查找,则 n 指向文档首,N 指向文档尾
2.3 插入
- i 当前字符前插入 insert
- a 当前字符后插入 append
- I 行首插入
- A 行尾插入
- o 在下一行插入
- O 在上一行插入
2.4 删除
- dd 删除一行并保存到剪切板 delete line (stored in local buffer)
- 3dd 删除三行
- dw delete word
- d) Delete to end of sentence
- d$ Delete all characters from cursor to end of line
- d- Delete current and previous line
- dfx Delete from cursor to first occurance of the letter “x”
- d’x Delete from the current line to the line marked with the identifier “x”
- ‘ad’b Delete from the line of mark “a” to the line marked “b”.
- d/cat Delete all characters from the cursor to the next occurance of (but not including) “cat”
- cc 删除一行并保存到剪切板,同时进入 insert 模式 copy line (stored in local buffer)
- 拓展可参照 dd
- s 删除当前字符 Substitute one character under cursor continue to insert
2.5 复制
- yy Yank (copy) current line into “unnamed” storage buffer.
- 扩展看参照 dd
2.6 粘贴
- p Paste unnamed storage buffer after current line.
- P Paste unnamed storage buffer before current line.
2.7 替换
r<X>将当前字符替换为 X Replace charactergu<X>将指定的文本转换为小写gU<X>将指定的文本转换为大写:%s/<search>/<replace>/查找 search 内容并替换为 replace 内容
2.8 撤销、重做
u撤销 Undo last change- U Undo all changes to entire line
Ctrl-c重做
2.9 保存文件
:w保存当前文件:wa保存全部文件:wqorZZ保存并退出:q!orZQ强制退出,不保存:saveas <new filename>文件另存为:w <new filename>文件另存一份名为<new filename>的副本并继续编辑原文件
3. 进阶操作
3.1 缩进
>>向右缩进当前行<<向左缩进当前行
3.2 自动排版
==自动排版当前行gg=G当前文档全文自动排版<N>==对从当前行开始的 N 行进行自动排版=<N>j对当前行以及向下 N 行进行自动排版=<N>k对当前行以及向上 N 行进行自动排版
3.3 分屏
:split缩写:sporCtrl-w s上下分屏:vsplit缩写:vsorCtrl-w v左右分屏:diffsplit缩写:diffsdiff 模式打开一个分屏,后面可以加上 {filename}
窗口跳转
Ctrl-w w激活下一个窗口Ctrl-w j激活下方窗口Ctrl-w k激活上方窗口Ctrl-w h激活左侧窗口Ctrl-w l激活右侧窗口
屏幕缩放
Ctrl-w =平均窗口尺寸Ctrl-w +增加高度Ctrl-w -缩减高度Ctrl-w _最大高度Ctrl-w >增加宽度Ctrl-w <缩减宽度Ctrl-w |最大宽度
3.4 标签页
创建标签页
:tabnewor:tabedit缩写:tabe打开新标签页Ctrl-w gf在新标签页中打开当前光标所在位置的文件名
注意::tabnew 和 :tabedit 后面都可以跟一个 <空格><文件名> 用以在新标签页中
打开指定文件,还可以在 : 后面加一个数字,指出新标签页在列表中的位置(从 0 开始)。
切换标签页
gtor:tabnext缩写:tabn下一个标签页(最后一个会循环到第一个)gTor:tabprevious缩写:tabp上一个标签页(第一个会循环到最后一个):tabrewind缩写:tabror:tabfirst缩写:tabfir到第一个:tablast缩写:tabl到最后一个标签页
关闭标签页
:tabclose缩写:tabc关闭当前标签页:-tabc关闭上一个标签页:+tabc关闭下一个标签页:tabonly缩写:tabo关闭其他标签页
4. Other
在 vim 中打开其他文件的办法
Hyper-Linking to include files:
- Place cursor over the file name (i.e.
#include "fileABC.h") - Enter the letter combination: gf
(go to file)
- Place cursor over the file name (i.e.
New session:
- Use command: “
:e filename“
Start new edit session on specified file name without closing current vi / vim editor process. - :Explore
List files in your current direct
- Use command: “
Buffer 的使用
buffer 就是当前 Vim session 的文件历史记录.
查看:
:buffers:ls
在 Buffer 之间跳转
:bn打开缓存中下一个文件:bp打开缓存中上一个文件:b<N>打开缓存中第 N 个文件
你也可以使用
:bdelete<N>来删除所要关闭的缓冲区,缩写:bd<N>。
g命令的使用
参考博客VIm 全局命令 g
重复上一次命令
.重复执行上一个命令。