Vim notes
5 modes
- normal (
Escorctrl+[): moving around a file - insert (
i): inserting text - replace (
R): replacing text - visual (plain
v, lineV, blockctrl+v, last blockgv): selecting blocks of text() - command-line (
:): running command
command-line mode
:qquit window:wwrite:wqwrite and quit:e {name of file}open file for editing:lsshow open buffers:help {topic}open help, such as:help :w,:help w:spseperate another window
movement in normal mode
-
Basic movements:
hjklleft down up right -
Words:
wnext words,bbegin of word,eend of word -
Lines:
0beginning of line,$end of line,^first non-blank character -
Screens:
Htop of screen,Mmiddle of screen,Lbottom of screen -
Scroll:
ctrl + dpage down,ctrl + upage up -
File:
ggbeginning of file,Gend of file -
Find:
f{c}forward find c,t{c}forward to c,F{c}backward find c,T{c}backward to cdifference between
fandt:fplaces the cursor on the found charactertpalces the cursor on the preceding character it found
ref: https://stackoverflow.com/questions/12495442/what-do-the-f-and-t-commands-do-in-vim
- Search:
/{regex},n/Nfor navigating matches. 搜索结束之后输入enter转入到第一个结果位置,之后输入n跳转到下一个结果位置,shift + njump to previous match. - Misc:
%find the coresponding item (找到相应的项,例如两个括号之间进行跳转)
selection in normal mode
- Visual:
v - Visual line:
V - Visual block:
ctrl + v - Last visual block:
gv
edit commands
iinsert at now cursoraappend at endo / Oinsert line down (below), up (above)d{motion}delete {motion}. such asdddelete this line,d$delete to end of line,d0delete to begin of line,dwdelete word,dGdelete to end of file,dldelete right characterc{motion}change {motion}.cwchange word etc,clequals tos,ccequals toSxdelete character (equals todl)sdelete character and start insert (substitute) (equals toxi, and short forcl)uundo,ctrl + rredoycopy. such asyycopy this line,ywcopy the word,y4lcopy right 4 charactersppaste~flips the case of a character (大小写转换).repeat previous editing command
count with manipulation
3wmove 3 words forward5jmove 5 lines down7dw / d7wdelete 7 words7yw / y7wcopy 7 words
vim extensions
since Vim8.0 不再需要额外的插件管理器,使用内置的即可。
将插件放在目录 ~/.vim/pack/vendor/start/ 下(可以使用 git clone 命令)。统一管理 dotfiles 之后安装插件方法见 piaoliangkb/dotfiles 仓库介绍的装方法 (install as git submodules).
https://missing.csail.mit.edu/2020/editors/#extending-vim
example install ctrlp:
- Install and configure a plugin:
ctrlp.vim. - Create the plugins directory with
mkdir -p ~/.vim/pack/vendor/start - Download the plugin:
cd ~/.vim/pack/vendor/start;git clone https://github.com/ctrlpvim/ctrlp.vim - Read the documentation for the plugin. Try using
CtrlPto locate a file by navigating to a project directory, opening Vim, and using the Vim command-line to start:CtrlP. - Customize
CtrlPby adding configuration to your ~/.vimrc to openCtrlPby pressingCtrl-P.
example install lightline:
- Install plugin
lightline.vim git clone https://github.com/itchyny/lightline.vim ~/.vim/pack/vendor/start/lightline
搜索和替换
:s (替换)命令(文档)。
-
%s/foo/bar/g- 在整个文件中将 foo 全局替换成 bar
-
%s/\[.*\](\(.*\))/\1/g- 将有命名的 Markdown 链接替换成简单 URLs
多窗口
- 用
:sp/:vsp来分割窗口 - 同一个缓存可以在多个窗口中显示。
宏
-
q{字符}来开始在寄存器{字符}中录制宏 -
q停止录制 -
@{字符}重放宏 -
宏的执行遇错误会停止
-
{计数}@{字符}执行一个宏{计数}次 -
宏可以递归
- 首先用
q{字符}q清除宏 - 录制该宏,用
@{字符}来递归调用该宏 (在录制完成之前不会有任何操作)
- 首先用
-
例子:将 xml 转成
json(file)-
一个有 “name” / “email” 键对象的数组
-
用一个 Python 程序?
-
用
sed/ 正则表达式g/people/d%s/<person>/{/g%s/<name>\(.*\)<\/name>/"name": "\1",/g- …
-
Vim 命令 / 宏
-
Gdd,ggdd删除第一行和最后一行 -
格式化最后一个元素的宏 (寄存器)
e- 跳转到有
<name>的行 qe^r"f>s": "<ESC>f<C"<ESC>q
- 跳转到有
-
格式化一个
的宏
- 跳转到有
<person>的行 qpS{<ESC>j@eA,<ESC>j@ejS},<ESC>q
- 跳转到有
-
格式化一个
标签然后转到另外一个的宏
- 跳转到有
<person>的行 qq@pjq
- 跳转到有
-
执行宏到文件尾
999@q
-
手动移除最后的
,然后加上[和]分隔符
-
-
扩展资料
vimtutor是一个 Vim 安装时自带的教程- Vim Adventures 是一个学习使用 Vim 的游戏
- Vim Tips Wiki
- Vim Advent Calendar 有很多 Vim 小技巧
- Vim Golf 是用 Vim 的用户界面作为程序语言的 code golf
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim(书籍)
课后练习
-
完成
vimtutor。 备注:它在一个 80x24(80 列,24 行) 终端窗口看起来效果最好。 -
下载我们提供的 vimrc,然后把它保存到
~/.vimrc。 通读这个注释详细的文件 (用 Vim!), 然后观察 Vim 在这个新的设置下看起来和使用起来有哪些细微的区别。 -
安装和配置一个插件:
ctrlp.vim- 用
mkdir -p ~/.vim/pack/vendor/start创建插件文件夹 - 下载这个插件:
cd ~/.vim/pack/vendor/start; git clone https://github.com/ctrlpvim/ctrlp.vim - 阅读这个插件的 文档。 尝试用
CtrlP来在一个工程文件夹里定位一个文件, 打开 Vim, 然后用 Vim 命令控制行开始:CtrlP. - 自定义
CtrlP: 添加 configuration 到你的~/.vimrc来用按 Ctrl-P 打开CtrlP
- 用
-
练习使用 Vim, 在你自己的机器上重做 演示。
-
下个月用 Vim 完成_所有的_文件编辑。每当不够高效的时候,或者你感觉 “一定有一个更好的方式”时, 尝试求助搜索引擎,很有可能有一个更好的方式。如果你遇到难题,可以来我们的答疑时间或者给我们发邮件。
-
在其他工具中设置 Vim 快捷键 (见上面的操作指南)。
-
进一步自定义你的
~/.vimrc和安装更多插件。 -
(高阶)用 Vim 宏将 XML 转换到 JSON (例子文件)。 尝试着先完全自己做,但是在你卡住的时候可以查看上面宏 章节。
Vimtutor笔记
| KEY | ACTION |
|---|---|
| x | 删除一次字符 |
| dw | 删除一个单词 |
| d$ | 从当前位置至行尾,全部删除 |
| p | 将删除的文本粘贴到光标行之后 |
| rx | 将光标处的字符替换为 x |
| ce | 替换单词,直到单词结尾 |
| CTRL-G | 显示光标所在行号和文件状态 |
| G | 跳转到最后一行 |
| number G | 跳转到第 number 行 |
| gg | 跳转到第一行 |
| / | 向前搜索 |
| ? | 向后搜索 |
| n | 搜索下一个 |
| N | 搜索上一个 |
| Ctrl-O | 返回较旧位置 |
| Ctrl-I | 跳转到较新位置 |
| % | 寻找匹配的 ), ], 或 } |
| :s/old/new/g | 使用 ‘new’ 替换 ‘old’ |
| :#,#s/old/new/g | #,# 是行号,替换两行之间的文本 |
| :%s/old/new/g | 全文范围内查找替换 |
| :%s/old/new/gc | 全文范围内查找替换,每次替换前需用户确定 |
!<cmd> |
执行外部命令 |
| :w FILENAME | 保存文件 |
| v | 开启可视化选择模式 |
| :r FILENAME | 在当前位置插入文件内容 retrieve |
| :r !ls | 插入当前目录下的文件列表 |
| o | 在当前行下方插入一行,并进入插入模式 |
| O | 在当前行上方插入一行,并进入插入模式 |
| R | 进入替换模式 |
| y | 拷贝 |
| :set ic | 忽略大小写 |
| :set noic | 大小写敏感 |
| :set hls | 开启高亮搜索 |
| :set ic | 开启递增搜索 |
| :help | 获取帮助 |
| Ctrl-W Ctrl-W | 在窗口间跳转 |
| :e ~/.vimrc | 编辑 vim 配置文件 |
| :r $VIMRUNTIME/vimrc_example.vim | 插入 vim 配置文件示例 |
| :help vimrc-intro | 查看 vim 配置文件的文档介绍 |
| :set nocp | 进入非兼容模式 |
Ctrl-D and <TAB> |
自动补全命令 |
很多修改文本的命令,通常包含一个操作符和一个动作。常见的动作如下表所示:
| KEY | MOTION |
|---|---|
| w | 直到下一个单词开始,不包含它的第一个字符 |
| e | 直到当前单词结尾,包含最后一个字符 |
| $ | 直到行尾,包含最后一个字符 |
补充:
dd: delete a whole line
2dd: delete two lines
0: move to the start of the line using a zero
U(capital): to undo all changes on a line
u(lowercase): to undo previous actio