在这篇中, 会列举各种实用的插件, 包括他们的安装, 配置及使用方法
注意: 不是本部分的所有插件都是你需要装的, 如果盲目安装插件只会导致你 vim 功能混乱, 速度底下, 所以适时整理真正需要的插件, 禁用或清除掉不常用的插件才是正确使用方法.
本系列教程共分为以下五个部分:
vim-plug
是 vim 下的插件管理器, 可以帮我们统一管理后续的所有插件, 后续的安装插件全部由此工具完成
类似的插件管理工具还有 Vundle, 相较而言 vim-plug
支持异步且效率非常高, 具体选择交由读者自己
终端中输入如下命令
1 | curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ |
PlugInstall
: 安装插件PlugUpdate
: 更新所有插件PlugUpgrade
: 更新插件本身PlugClean
: 删除插件, 把安装插件对应行删除, 然后执行这个命令即可安装完 vim-plug
之后, 我们就可以使用其为我们服务安装插件了, 我们只需要在 call plug#begin(~/.vim/plugged)
与 call plug#end()
中指明我们需要的第三方插件即可, 如下:
1 | call plug#begin('~/.vim/plugged') |
后续的所有插件除非特别说明, 否则都按照 Plug 'PlugName'
的方式进行安装
自动格式化管理插件, 可根据不同文件类型使用不同的格式化工具
1 | Plug 'Chiel92/vim-autoformat' |
1 | "***************** vim-autoformat ********************** |
与 AutoFormat
插件类似, 本插件也相当于一个错误统计处理平台, 通过为不同语言配置不同 linter 来达到错误统计的效果
如果你安装了 lightline 或者其他的类似工具, 那么也可以集成到你的底部工具栏中
1 | Plug 'dense-analysis/ale' |
1 | "********************* dense-analysis/ale ************************ |
1 | " 快速跳转至错误的快捷键 |
很多人使用 vim 编辑文件, 完成后退出 vim 进行目录切换, 殊不知 vim 其实自带路径管理功能的, 从 vim 7 以后我们可以使用 vim 自带的 netrw
进入路径管理窗口
netrw
是 vim 自带的插件, 不需要额外安装, 其提供的功能非常强大, 相比与 NERDTREE
这些第三方插件来说速度更快, 体量更轻, 设计更简洁
:Ex
: 全屏进入 netrw
, 全称是 :Explorer
:Sex>
: 水平分割进入 netrw
:Vex>
: 垂直分割进入 netrw
<F1>
: 在 netrw 界面弹出帮助信息<CR>
: 打开光标下文件 / 夹-
: 进入上一级目录p
: 预览文件 (光标保持不动)P
: 打开文件, 会在上一个使用的窗口一侧的第一个窗口打开文件<C-w>z
: 关闭预览窗口gn
: 使光标下的目录作为目录树最顶部, 在 tree style 下与 <CR>
是不同的d
: 创建文件夹D
: 移除文件 / 夹cd
: change 工作目录到当前路径I
: 显示 / 隐藏顶部 banner
o
: 以水平分割窗口方式打开光标下文件v
: 以垂直分割窗口方式打开光标下文件%
: 在当前目录下新建一个文件并编辑r
: 翻转排序方式qb
: 列出所有的目录以及历史路径qf
: 显示文件详细信息R
: 重命名文件 / 文件夹s
: 在 name
, time
和 file size
之间切换排序t
: 新 tab 中打开文件<c-h>
: 编辑隐藏列表<c-l>
: 更新 netrw 列表内容a
: 隐藏 / 显示由 g: netrw_list_hide
所控制的文件C
: 设置编辑窗口gb
: 跳转到上次标记的书签gd
: 强制作为目录gf
: 强制作为文件gh
: 快速隐藏 .
开头的文件i
: 在 thin
, long
, wide
, tree listings
状态之间切换mb
: 将当前目录存为书签mc
: Copy marked files to marked-file target directorymm
: Move marked files to marked-file target directorymd
: 对标记的文件做 diff
操作me
: 将标记的文件放入参数列表中并进行编辑mf
: 标记一个文件mF
: 取消标记一个文件mg
: 对标记的文件使用 vimgrep
命令mp
: 打印标记的文件mr
: 使用 shell-style
标记文件mt
: 使当前目录成为标记文件目标mT
: 对标记文件应用 ctags
mu
: 对所有标记文件取消标记mz
: 压缩 / 反压缩标记文件O
: Obtain a file specified by cursorqF
: Mark files using a quickfix listS
: 确认在 name 排序状态下的扩展名优先级u
: 跳转到上一次浏览的目录x
: 使用系统中与之关联的程序打开光标下文件X
: 执行光标下的文件1 | let g:netrw_hide = 1 "设置默认隐藏 |
netw 复制文件的方式比较费解, 其原理是先标记好一个源文件, 然后标记好一个要被拷贝到的路径, 最后使用拷贝命令进行拷贝, 具体如下:
mf
标记源文件./
上, 然后使用 mt
标记此目标路径mc
拷贝文件至此 (也可以 mv
移动文件至此)不管出于任何原因不想使用 netrw
, 我们都有很多第三方插件可以选择, NerdTree
就是其中的佼佼者
先在.vimrc 文件中添加 Plug 名称及设定:
1 | Plug 'preservim/nerdtree' |
运行 vim, 输入命令 :PlugInstall
文件操作
e
: 进入文件夹内部浏览, 会在右侧开启小窗口进入文件夹列表o
: 在预览窗口中打开文件, 左侧 NerdTree 仍然被保留 (事实上除非打开新 tab 或手动退出, 否在会一直存在)O
: 递归地打开其内所有文件夹go
: 在预览窗口中打开文件, 光标将仍然保留在小窗口中, 非常好用, 用于预览多个文件特别有用.i
: 以分割视图打开文件gi
: 以分割视图打开, 但是光标仍然保留在小窗口s
: 以分割视图打开文件gs
: 以分割视图打开文件, 但是光标仍然保留在小窗口t
: 在新标签页打开选择的文件, 全屏T
: 在新标签页静默打开选择的文件, 全屏, 因为是静默, 所以不会跳转到新窗口C
: 将当前所选文件夹改为根目录, 即进入到所选择的文件夹, 与 o 不同, o 是在当前视图下将文件夹展开, C 则是直接进入到文件夹.cd
: 将当前文件夹改为 cwd(当前工作目录)CD
: 将文件夹目录跳转到 CWD(当前工作目录) 中m
: 对所选择的文件或文件夹弹出编辑菜单. 包括修改文件名, 复制, 移动, 删除等操作B
: 隐藏 / 显示书签, 如果显示书签, 还会将光标自动跳转至书签I
: 显示系统隐藏文件关闭移动系列
q
: 直接退出 NerdTreeD
: 删除书签F
: 隐藏文件, 只保留文件夹在视图中⌃ j
: 当同一个 NerdTree 有多个目录级别时, 只在同一级别下向下移动⌃ k
: 当同一个 NerdTree 有多个目录级别时, 只在同一级别下向上移动J
: 移动到同一级别的最下方K
: 移动到同一级别的最上方其他
A
: 全屏进入 NerdTree 窗口r
: 刷新当前文件夹的缓存, 使界面刷新R
: 刷新整个文件夹树的缓存, 使整个界面更新?
: 快速显示帮助, 非常有用, 忘记功能时使用!每次
NERDTree
从左侧显示出来的时候其所在目录即工作目录, 可以通过cd
命令进行设置, 或者在.vimrc
中设置set autochair
进行自动切换, 这个概念对于文件批量操作很重要, 因为文件批量操作时添加待操作文件是依靠当前工作目录来进行筛选的.
一个映射了大量实用命令的插件, 主要前缀键是 [
与 ]
,
1 | Plug 'tpope/vim-unimpaired' |
[b
:bprevious]b
:bnext[B
:bfirst]B
:blast[Space
: 当前行上增加空行]Space
: 当前行下增加空行[e
: 当前行上移]e
: 当前行下移[f
:previous file in current directory]f
:next file in current directory<p
: 复制到当前行下, 减少缩进<P
: 复制到当前行上, 减少缩进=P
: 复制到当前上, 自动缩进>p
: 复制到当前行下, 增加缩进>P
: 复制到当前行上, 增加缩进[p
: 复制到当前行上]p
: 复制到当前行下[q
:cprevious, quickfix previous]q
:cnext[Q
:cfirst]Q
:clast[a
:previous]a
:next[A
:first]A
:last[l
:lprevious]l
:lnext[L
:lfirst]L
:llast[<C-L>
:lpfile]<C-L>
:lnfile[<C-Q>
:cpfile]<C-Q>
:cnfile[<C-T>
:ptprevious]<C-T>
:ptnext[n
:previous scm conflict]n
:next scm conflict[t
:tprevious, tag previous]t
:tnext[T
:tfirst]T
:tlastvim 下的输入法自动切换工具, 在进入命令模式时自动切换至英文输入法, 回到插入模式时返回到上一次选择的输入法 (在需要中英文切换的环境中非常有用)
下载基础工具 xkbswitch-macosx(每个系统有不同的实现工具, 这里以 macOS 为例)
1 | git clone https://github.com/myshov/xkbswitch-macosx |
在 vim 中安装插件
在 vimrc
中加入下面的内容:
1 | Plug 'lyokha/vim-xkbswitch', {'as': 'xkbswitch'} |
最后 PlugInstall
即可.
先在.vimrc 文件中添加 Plug 名称及设定:
1 | Plug 'lyokha/vim-xkbswitch', {'as': 'xkbswitch'} |
运行 vim, 输入命令:PlugInstall
在 normal
模式下手动切换至英文输入法模式, 然后进入 insert
模式后手动切换到中文输入法模式, 此时插件已经记忆了输入法的状态了, 在 ESC
回到 normal
模式后会自动切换到英文输入法模式, 再次进入 insert
模式时会自动切换到中文输入法模式
自动补全匹配符号
1 | Plug 'jiangmiao/auto-pairs' |
1 | let g:AutoPairsMapCR = 0 |
<M-p>
: Toggle Autopairs (g:AutoPairsShortcutToggle)<M-e>
: Fast Wrap (g:AutoPairsShortcutFastWrap)<M-n>
: Jump to next closed pair (g:AutoPairsShortcutJump)<M-b>
: BackInsert (g:AutoPairsShortcutBackInsert)<M-(>
/ <M-)>
/ <M-[>
/ <M-]>
/ <M-{>
/ <M-}>
/ <M-">
/ <M-'>
: Move character under the cursor to the pair一款对齐插件, 快速按照给定的分隔符号完成指定范围内的对齐操作
1 | Plug 'godlygeek/tabular' |
:Tabularize /,/
: 将整个缓冲区的所有行按照 ,
符号进行对齐
:'<,'>Tabularize /,/
: 对高亮选中范围内的行进行对齐
:Tabularize /,/l1c1r0
: 按照 ,
进行对齐, 并且为每个分割的文本区域内的文本指定对齐方式, l
, c
, r
分别为左中右对齐, 1
代表每个分隔区域对齐补全后添加一个空格
1 | abc,def,ghi |
- 对于分隔符所处的区域,
l
/r
/c
的作用是相同的, 因为其只有一个宽度- 如果分隔的区块足够多, 那么将会循环使用
r1c1l0
1 | :Tabularize /,/r1c1l0 |
1 | Plug 'boydos/emmet-vim' |
1 | let g:user_emmet_mode='a' "enable all function in all mode. |
div>ul>li
+ <C-y>,
: >
生成子节点
1 | <div> |
div+p+bq
+ <C-y>,
: +
生成兄弟节点
1 | <div></div> |
div+div>p>span+em^bq
+ <C-y>,
: ^
与 >
相反, 在父节点生成新节点
1 | <div></div> |
div+div>p>span+em^^^bq
+ <C-y>,
: 使用 n 个 ^
, 就可以在第 n 父级生成新的节点
1 | <div></div> |
ul>li*5
+ <C-y>,
: 使用 *
生成多个相同元素
1 | <ul> |
div>(header>ul>li*2>a)+footer>p
+ <C-y>,
: 圆括号 ()
是 Emmet 的高级用法, 用来实现比较复杂的 DOM 结构
1 | <div> |
(div>dl>(dt+dd)*3)+footer>p
+ <C-y>,
: 还可以嵌套使用圆括号 ()
1 | <div> |
div#header+div.page+div#footer.class1.class2.class3
+ <C-y>,
: Emmet 给元素添加 ID 和 CLASS 的方法和 CSS 的语法类似
1 | <div id="header"></div> |
td[title="Hello world!" colspan=3]
+ <C-y>,
: 使用 [attr]
标记来添加自定义属性
1 | <td title="Hello world!" colspan="3"></td> |
ul>li.item$*5
+ <C-y>,
: 使用 $
操作符可以对重复元素进行有序编号
1 | <ul> |
ul>li.item$$$*5
+ <C-y>,
: 还可以用多个 $
定义编号的格式
1 | <ul> |
ul>li.item$@-*5
+ <C-y>,
: 使用 @
修饰符可以改变编号的格式, 在 $
后面添加 @-
可以改变编号顺序
1 | <ul> |
ul>li.item$@3*5
+ <C-y>,
: 在 $
后面添加 @N
可以改变编号基数
1 | <ul> |
ul>li.item$@-3*5
+ <C-y>,
: 还可以组合使用上面的修饰符
1 | <ul> |
a{click}+b{here}
+ <C-y>,
: Emmet 使用 Text:{}
给元素添加文本内容
1 | <a href="">click</a> |
a>{click}+b{here}
+ <C-y>,
:
1 | <a href="">click<b>here</b></a> |
p>{Click }+a{here}+{ to continue}
+ <C-y>,
:
1 | <p> |
#page>div.logo+ul#navigation>li*5>a{Item $}
+ <C-y>,
:
1 | <div id="page"> |
<Ctrl-y>,
: 展开简写式<Ctrl-y>d
: Balance a Tag Inward(选中包围的标签?)<Ctrl-y>D
: Balance a Tag Outward<Ctrl-y>n
: 进入下个编辑点<Ctrl-y>N
: 进入上个编辑点<Ctrl-y>i
: 更新 <img>
图像尺寸<Ctrl-y>m
: 合并文本行<Ctrl-y>k
: 删除标签<Ctrl-y>j
: 分解 / 展开空标签<Ctrl-y>/
: 注释开关<Ctrl-y>a
: 从 URL 生成 anchor 标签<Ctrl-y>A
: 从 URL 生成引用文本一款在浏览器中预览 markdown 文件的插件
1 | Plug 'iamcco/markdown-preview.nvim', {'do': 'cd app & yarn install'} |
运行 vim, 输入命令:PlugInstall
1 | map <F3>:MarkdownPreview<CR> "设置 F3 开启 Markdown 文件预览 |
:MarkdownPreview
: 开启预览:MarkdownPreviewStop
: 停止预览:MarkdownPreviewTroggle
: 开关预览在窗口右侧显示 markdown 目录结构的一个插件, 此插件基于 ctags 和 tagbar(Tagbar 是一个著名的文档目录显示插件, 但是不支持 markdown, 此插件在 Tagbar 的基础上添加了对 markdown 的支持). 因此此插件必须同时安装以上两种插件方可正常工作
通过 brew
安装 ctgs
1 | brew install ctags |
在 .vimrc
文件中添加 Plug
名称
1 | Plug 'jszakmeister/markdown2ctags' |
1 | "***************** Tagbar ************************************* |
只要在 vim 界面中使用 :TagbarToggle
即可调出 Tagbar 界面, 即可显示 markdown 的目录结构.
一款 markdown 语法检查工具, 可以根据预设的规则进行 markdown 语法错误警告或提示, 可根据需要进行规则自定义
1 | brew install markdownlint-cli |
配合 ALE 插件一起使用
1 | let g:ale_linters = { |
在项目根目录下建立 .markdownlint.json
配置文件, 在其中对默认的规则进行配置, ale markdownlint 工具在被调用的时候会自动去查找该名称配置文件
1 | { |
fuzzy find
, 快速模糊搜索查找工具
fzf.vim 与 终端工具 fzf 配合使用, 在 vim 中的
:FZF
与Files
命令都会调用export FZF_DEFAULT_COMMAND='...'
这个参数, 需要在.zshrc
中配置好
在终端中安装 fzf 工具
1 | brew install fzf |
~/.vimrc
中安装 vim 的 fzf
插件
1 | Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } |
首先在 .zshrc
中配置终端中的 fzf 选项, 如下:
1 | export FZF_DEFAULT_COMMAND="fd --hidden --follow -I --exclude={Pods,.git,.idea,.sass-cache,node_modules,build} --type f" |
(fzf 可扩展性很高, 如果进行适当配置, 它可以在你进行路径跳转, 历史命令搜索, 文件搜索等方面给你极大的帮助!)
然后在 vim 中配置 fzf 插件的相关设置
1 | nnoremap <Leader>fh:History<CR> |
:Files [path]
: 列出 path 路径下的所有文件 (功能等价于 :FZF
命令)
:Buffers
: 文件缓冲区切换
:Colors
: 选择 Vim 配色方案
:Tags [QUERY]
: 当前项目中的 Tag (等价于: ctags -R)
:BTags
: [QUERY] 当前活动缓冲区的标记
:Marks
: 所有 Vim 标记
:Windows
: 窗口
:Lines [QUERY]
: 在所有加载的文件缓冲区里包含目标词的所有行
:BLines [QUERY]
: 在当前文件缓冲区里包含目标词的行
:Locate PATTERN
: locate command output
:History
: v:oldfiles and open buffers
:History:
: 命令行命令历史
:History/
: 搜索历史
:Commands
: Vim 命令列表
:Maps
: 普通模式下的按键映射
:Snippets
: Snippets ([UltiSnips][us])
:Commits
: Git commits (requires [fugitive.vim][f])
:BCommits
: 查看与当前缓冲区有关的 commit
:GFiles [OPTS]
: Git files (git ls-files)
:GFiles?
: Git files (git status)
:Ag [PATTERN]
: ag search result (ALT-A to select all, ALT-D to deselect all)
:Rg [PATTERN]
: rg search result (ALT-A to select all, ALT-D to deselect all)
:Filetypes
: File types
补全插件, 支持 c
, c++
, java
, python
, PHP
等多语言
先在 .vimrc
文件中添加 Plug 名称
1 | Plug 'ycm-core/YouCompleteMe'` |
运行 vim, 输入命令 :PlugInstall
经历过上述 2 个步骤后, YouCompleteMe 插件还没法使用, 此时打开 Vim 时会看到如下的报错:
1 | The ycmd server SHUT DOWN (restart with ':YcmRestartServer’). YCM core library not detected; you need to compile YCM before using it. Follow the |
这是因为, YouCompleteMe 需要手工编译出库文件 ycm_core.so (以及依赖的 libclang.so) 才可使用. 假设使用 vim-plug 下载的 YouCompleteMe 源码保存在目录 ~/.vim/plugged/YouCompleteMe, 在该目录下执行
1 | # 编译全部语言 |
基于 lsp 的补全插件, 基本上支持 lsp 的语言都可以使用此插件进行补全. 此插件利用了 vsc 的插件生态, 方案比较成熟, 推荐使用 (作者是国人)
CocInstall <plugin>
: 安装插件CocUninstall
: 卸载插件CocConfig
: 打开配置文件 (vim: ~/.vim/coc-settings.json
)CocLocalConfig
: 打开本地配置文件CocEnable
: 开启 cocCocDisable
: 关闭CocUpdate
: 升级插件CocList <flag>
: 列出相关内容diagnostic
: 诊断信息extension
: 所有插件commands
: 所有可用命令outline
: 大纲symbols
: symbols1 | coc-clangd |
管理 tag 文件, tag 文件关乎着项目的引用与跳转, 因此是一个比较大的话题, 详细可以参考 韦大的文章
安装 universal-ctags
命令行程序
1 | brew tap universal-ctags/universal-ctags |
安装 vim 插件 vim-gutentags
1 | Plug 'ludovicchabant/vim-gutentags' |
1 | "***************** gutentags ************************************* |
默认情况下, 我们不能跳转到 printf
这类标准库中的方法中, 如果需要的话, 我们可以为系统标准库生成 tags, 然后将在 .vimrc
文件中为其进行指定, 这样即可跳转到系统的标准库头文件定义中了.
首先将系统中的头文件目录找出来, 然后使用 ctags 对目录中所有文件进行生成
1 | ctags --fields=+niazS --extras=+q --c++-kinds=+px --c-kinds=+px --output-format=e-ctags -R -f ~/.vim/systags /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include ~/header |
然后在 vim
中指定 tags 文件的路径即可
1 | set tags+=~/.vim/systags |
vim 下最好的 git 插件, 同时也是 git 下最好的 vim 插件
fugitive 插件操作的对象名为 fugitive object
, 可以是文件也可以是 commit, 下面列举了一些 fugitive object
的表示方式
@
: The commit referenced by @ aka HEADmaster
: The commit referenced by mastermaster^
: The parent of the commit referenced by mastermaster...other
: The merge base of master and othermaster:
: The tree referenced by master./master
: The file named master in the working directory:(top)master
: The file named master in the work treeMakefile
: The file named Makefile in the work tree@^:Makefile
: The file named Makefile in the parent of HEAD:Makefile
: The file named Makefile in the index (writable)@~2:%
: The current file in the grandparent of HEAD:%
: The current file in the index:1:%
: The current file's common ancestor during a conflict:2:#
: The alternate file in the target branch during a conflict:3:#5
: The file from buffer #5 in the merged branch during a conflict!
: The commit owning the current file!:Makefile
: The file named Makefile in the commit owning the current file!3^2
: The second parent of the commit owning buffer #3.git/config
: The repo config file:
: The |fugitive-summary| buffer-
: A temp file containing the last |:Git| invocation's output<cfile>
: The file or commit under the cursor:Git
: 进入 summary 界面:Git <arbitrary subcommand>
: 所有 command line 中 git...
后面可以使用的 subcommand 都可以使用, 比如 :Git push
, :Git push
, 甚至是在 ~/.gitconfig
中的 git alias
.:Git <arbitrary subcommand> -p
: 与上命令相同, 不过会将命令结果单独开一个页面进行显示:Git blame
: 对当前文件执行 git blame
命令:Gclog[!]
: 将本 repo 的所有 log 输出至 quickfix, 并跳转至第一个 commit 的信息页面 (添加 !
可以防止跳转):Gllog[!]
: 与 Gclog
相同, 但是将结果输出至 location list
:[range]Gclog[!]
: 给定范围 (比如选中多行), 然后使用 Gclog
的话会将与范围相关的所有 commit 列出, 可以使用 :0Gclog!
来将与本文件相关的所有 commit 列出:Gread [object]
: 如果不传 fugitive object
, 则等同于 git checkout -- file
, 如果传了, 则先将本 buffer 清空, 然后读取指定的 fugitive-object
内容到本 buffer 中:Gwrite
: 类似于 git add
:Gcd [directory]
: cd 到本 root 的根目录下:Gedit [object]
: edit 一个 fugitive object
:Gdiffsplit [object]
: 使用 vimdiff
查看给定的 object
与当前 file
的差异, 如果给定的是一个 commit
, 那么会将该 commit 中的本文件与当前本文件进行差异对比:Gvdiffsplit [object]
: 与 :Gdiffsplit
相同, 但是永远 split vertically.:Ghdiffsplit [object]
: 与 :Gdiffsplit
相同, 但是永远 split horizontally.:GMove {destination}
Wrapper around git-mv that renames the buffer afterward. Add a! to pass -f.:GRename {destination}
Like |:GMove| but operates relative to the parent directory of the current file.:GDelete
: 与 git rm --cached **
相同, Add a! to pass -f and forcefully discard the buffer.:GRemove
: 与 GDelete
相同, 但是保持空 buffer 存在:GBrowse
: 在 GitHub 中查看当前 file / commitA
: resize to end of author columnC
: resize to end of commit columnD
: resize to end of date/time columngq
: close blame, then :Gedit
to return to work tree version<CR>
: close blame, and jump to patch that added line (or directly to blob for boundary commit)o
: jump to patch or blob in horizontal splitO
: jump to patch or blob in new tabp
: jump to patch or blob in preview window-
: reblame at commit~
: reblame at [count]th first grandparentP
: reblame at [count]th parent (like HEAD^[count])s
: Stage (add) the file or hunk under the cursor.u
: Unstage (reset) the file or hunk under the cursor.-
: Stage or unstage the file or hunk under the cursor.U
: Unstage everything.X
: Discard the change under the cursor.=
: Toggle an inline diff of the file under the cursor.>
: Insert an inline diff of the file under the cursor.<
: Remove the inline diff of the file under the cursor.gI
: Open.git/info/exclude in a split and add the file under the cursor. Use a count to open.gitignore.I
: Invoke |:Git| add --patch or reset --patch on the fileP
: under the cursor. On untracked files, this insteadgq
: Close the status buffer.dd
: Perform a |:Gdiffsplit| on the file under the cursor.dv
: Perform a |:Gvdiffsplit| on the file under the cursor.ds
: Perform a |:Ghdiffsplit| on the file under the cursor.<CR>
: Open the file or |fugitive-object| under the cursor. In a blob, this and similar maps jump to the patch from the diff where this was
added, or where it was removed if a count was given. If the line is still in the work tree version, passing a count takes you to it.o
: Open the file or fugitive-object
under the cursor in a new split.gO
: Open the file or fugitive-object
under the cursor in a new vertical split.O
: Open the file or fugitive-object
under the cursor in a new tab.p
: Open the file or fugitive-object
under the cursor in a preview window. In the status buffer, 1p is required to bypass the legacy usage instructions.~
: Open the current file in the [count]th first ancestor.P
: Open the current file in the [count]th parent.C
: Open the commit containing the current file.:Gdiffsplit HEAD
:0Gclog!
查看当前 file 之前某个版本与现在版本的差异
:0Gclog!
列出所有本 file 相关的 commit
enter
进入需要对比的 commit
Gdiffsplit HEAD
与当前 HEAD
对比差异
我们也可以直接找出需要对比的 commit hash, 让后直接使用
Gdiffsplit commit_hash
来进行对比. 当进行 diff 对比时, 永远是旧的 commit 信息列在屏幕左侧, 新的 commit 信息列在右侧
一款基于 fugitive
的查看 vim commit 树形图的工具
:GV
: to open commit browser:GV!
: will only list commits that affected the current file:GV?
: fills the location list with the revisions of the current fileo
/ <cr>
on a commit to display the content of ito
/ <cr>
on commits to display the diff in the rangeO
: opens a new tab insteadgb
: for:Gbrowse]]
: and [[ to move between commits.
: to start command-line with:Git [CURSOR] SHA à la fugitiveq
: or gq to close一款超级强大的快速添加 / 删除 / 改变包围符号的神器
1 | Plug 'tpope/vim-surround' |
ds
: 删除包围符号cs
: 改变包围符号ysw
: 当前至下一个词尾添加一个包围符号ysW
: 当前至至下一个空格添加一个包围符号ySw
: 当前至下一个词尾添加一个包围符号并将焦点移至下一行ySW
: 当前至下一个空格添加一个包围符号并将焦点移至下一行yss)
: 整行添加包围符号 ()
ysiw)
: 为当前光标下单词添加包围符号 ()
S"
: Visual 模式下对选中区域添加包围符号 "
gS"
: Visual 模式下对选中区域进行换行并添加包围符号⌃-s
: Insert 模式下插入包围符号⌃-s, ⌃-s
: Insert 模式下在插入包围符号并将焦点移至下一行dst
: 删除 html/xml 的标签内部的所有字符cst
: 删除 html/xml 的标签内部的所有字符并进入插入模式ysa<'
: 在 <>
包裹的范围上加符号 '
1 | | Old text | Command | New text | |
快速注释插件
1 | Plug 'preservim/nerdcommenter' |
<leader>cc
: NERDCommenterComment, 注释当前行或所选择行 (文本)<leader>cu
: NERDCommenterUncomment, 取消当前所处位置的注释状态 Uncomments the selected line(s).<leader>ci
: NERDCommenterInvert, 反转所选择行的注释状态 (逐个地反转) 仅支持行
<leader>c<space>
: NERDCommenterToggle (反) 激活所选择行的注释状态, 依据最顶部行的注释状态进行判断, 执行命令后, 所选择行的注释状态均为最顶部行注释状态的相反状态. 仅支持行
<leader>cn
: NERDCommenterNested, 与 cc 相同, 不过嵌套地进行注释<leader>cs
: NERDCommenterSexy, 将当前选择文本以块的方式进行注释 (即在选择文本的上方与下方加上单行注释) 仅支持行
<leader>cy
: NERDCommenterYank, 与 cc 完全相同, 不过会先进行复制操作<leader>c$
: NERDCommenterToEOL Comments the current line from the cursor to the end of line.<leader>cA
: NERDCommenterAppend, Adds comment delimiters to the end of line and goes into insert mode between them.<leader>ca
: NERDCommenterAltDelims Switches to the alternative set of delimiters.<leader>cm
: NERDCommenterMinimal, Comments the given lines using only one set of multipart delimiters.快速注释插件, 相比于 nerdcommenter
更加简洁实用
1 | Plug 'tpope/vim-commentary' |
gcc
: 注释或反注释gcap
: 注释一段gc
: visual 模式下直接注释所有已选择的行实现真正的多光标的一个插件, vim 的 visual block 模式并不是多光标, 如果想将 visual block 模式下被选中的多行的当前单词推进到每个单词的末尾, 那么就需要使用到多光标的概念.
我理解的此多光标插件的使用分为两种状态
- 从 normal 模式直接使用
<C-n>
进入多光标状态并选中当前光标下的单词, 然后再次使用<C-n>
选择下一个,<C-x>
跳过当前符合的单词, 最后进行插入修改等操作- 从 visual 或 visual block 模式下使用
<C-n>
进入直接添加光标到当前所有行的选中单词处, 然后移动光标, 在合适位置进行进行插入修改等操作, 最后 esc 两次退出
1 | Plug 'terryma/vim-multiple-cursors' |
<C-n>
: 进入多光标状态 / 或选择下一个符合当前选择的单词<C-x>
: 跳过当前候选<C-p>
: 移除当前单词处的光标及选择状态跳转到上一个光标处1 | let g:multi_cursor_use_default_mapping=0 |
1 | Plug 'mg979/vim-visual-multi', {'branch': 'master'} |
<C-n>
: 选择当前光标所在的单词, 进入 V-M
(visual multi) 模式<C-Down>
/ <C-Up>
: 创建垂直光标选区<S-Arrows>
: 一次创建一个字符选区n
/ N
: 选择下一个出现的相同字符[/
/ ]
: 跳转到上一个 / 下一个选区处q
: 跳过当前并选择下一个出现的地方Q
: 移除当前的选取i
/ a
/ I
/ A
: 进入插入模式<Tab>
: 在 cursor mode
与 extend mode
之间切换在
V-M
模式中, 绝大多数 vim 命令都是可以使用的, 比如r
,~
一个异步批量搜索替换工具
1 | Plug 'dyng/ctrlsf.vim' |
CtrlSF [pattern]
: 搜索匹配字符串CtrlSF window
中:<CR>
/ o
: 在相应的文件中打开相应的行<C-O>
: 在 horizontal split window
中打开t
: 在新 tab 中打开p
: 在 preview 中打开P
: 在 preview 中打开并将焦点移动到 preview 上O
: 与 o
相同, 但是保持 CtrlSF
开启T
: 与 t
相同, 但是保持 focus 在 CtrlSF 上M
: 在 normal view
(sublime) 与 compact view
(quick-fix) 切换q
: 退出 CtrlSF<C-j>
: 移动光标到下一个匹配处<C-k>
: 移动光标到上一个匹配处<C-c>
: 停止搜索preview window
中q
: 关闭 preview
一套配色 vim 配色方案, 韩国人出品. 与之匹配的还有一个 iTerm 配色方案, 两者结合的比较好看
在.vimrc 文件中添加 Plug 名称及设定:
1 | Plug 'junegunn/seoul256.vim' |
一款专注写作的 vim 插件, 开启后四周空白, 更利于专注. 不适用于写代码和看代码
在.vimrc 文件中添加 Plug 名称及设定:
1 | Plug 'junegunn/goyo.vim' |
:Goyo
: 进入专注模式:Goyo!
: 退出专注模式, 或使用:q:Goyo 90%
: 调整高度为窗口的 90%:Goyo x30%
: 调整宽度为窗口的 30%:Goyo 70%-10x90%+10%
: 调整区域宽为窗口 70%, 左边距向左移 10 单位, 高度为窗口 90%, 向下移动窗口的 10%与 Goyo
, seoul256
为同一开发者, 联合使用效果最佳. 不适用于写代码和看代码.
在 .vimrc
文件中添加 Plug
名称及设定:
1 | Plug 'junegunn/limelight.vim' |
:Limelight
// 进入 Limelight 状态:Limelight!
// 退出 Limelight 状态:Limelight0.3
//如果你同时是一名 iOS 开发者, 那么 XVim
可以帮助你在 Xcode
中找回缺失的 Vim
操作, XVim 可以让 Xcode 像 vim 一样编辑.
由于 XVim 没有上架到 Mac App Store, 因此我们需要进入官网下载源码编译按照, 编译前需要对 Xcode 进行自签名, 否则我们自己编译出来的结果文件是不能安装到 Xcode 上的
对系统的代码证书重新生成
克隆源码仓库
1 | git clone https://github.com/XVimProject/XVim2.git |
确认 xcode 内容点
1 | xcode-select -p |
如果有多个版本 Xcode, 此项会让你清楚你将安装 XVim 到哪一个版本上, 如果结果不是你想要的版本, 那么使用 xcode-select -s <path-of-xcode>
进行手动指定
make
1 | cd XVim2 |
按照需要可生成 .xvimrc
文件 ( .xvimrc
文件必须放在用户主目录, 即 .vimrc
同级目录)
重启 Xcode
完成安装
:run
: xcode 代码运行:make
: 构建 xcode 代码:xhelp
: 光标位置快速帮助:xccmd
: 执行 xcode 菜单⌃ g
: 打印当前行的位置Vimum
是一款 Chrome
插件, 使用 vim 的模式概念让我们可以脱离鼠标访问浏览网页
gg
: 跳转到页面顶部G
: 跳转到页面底部gi
: 激活搜索框j
: 页面向下滚动k
: 页面向上滚动u
: 页面向上翻页d
: 页面向下翻页r
: 刷新当前页面H
: 页面回退到上一次历史L
: 页面从历史记录中返回来x
: 关闭页面X
: 恢复被关闭的页面 (可多次重复)f
: 显示页面上各个点击点的链接, 可以在当前页打开F
: 显示页面上各个点击点的链接, 在新页面打开gt
: 向右侧浏览下一个 tabgT
: 向左侧浏览 tabyf
: 拷贝页面上显示的链接yy
: 拷贝当前网页的链接yt
: 复制当前 tabv
: 进入选择模式, 可选择文本, 第一次按下时会进入 creat mode
, 选中起点后再次按下 v
将启用选择模式, 然后按下 y
来进行复制. 如果需要再次进入
creat mode
, 可使用 c
按键. 如果在复制中要改变复制区域的起点, 可以使用 o
按键, 或者在使用 /
进行搜索确定焦点后进行 v
选择操作.
在选择模式中可使用 w
, b
, h
, j
, k
, l
, e
, $
来进行移动V
: 进入行选择模式, 可批量选择多行文本o
: 键入搜索内容, 可在当前页面显示历史记录或打开网页链接或搜索新内容O
: 兼容搜索内容, 在新页面给出与 o 键相同的结果b
: 搜索书签, 并在当前页面打开B
: 搜索书签, 并在新页面打开T
: 在已打开的 tab 中进行搜索/
: 搜索当前页面值, 使用 ⌘ F
进行也页面搜索, /
不能搜索中文n
: 选中下一个搜索结果N
: 选中上一个搜索结果如果你正在使用 Safari
, 但是也想在浏览器中使用 vim 的操作, 那么 Vimari
就很适合你了, 因为 Vimum
不支持 Safari
, 因此 Vimari
就诞生出来了,
虽然没有 Vimum
功能那么强大, 但是基本的浏览操作倒是都覆盖了
f
: 触发跳转F
: 触发跳转 (新 tab 中打开链接)h/j/k/l
: 移动u
: 向上翻页d
: 向下翻页gg
: 跳转到页面顶部G
: 跳转到页面底部gi
: 跳转到第一个输入处H
: 回到前一个历史页面L
: 回到后一个历史页面r
: 重载页面w
: 下一个 tabq
: 上一个 tabx
: 关闭当前 tabt
: 开启新 tabvim 丰富的插件生态是其一大特色, 本文只是起到抛砖引玉的功能, 更多实用的插件有待读者的发现. 以下列举了笔者常用的插件.
1 | "============= File Management ============= |
实际上以上所列出的插件很多仅由百余行的文件构成, 所以如果一些插件不能满足需求的话完全可以按照自己的想法写出一个适合自己的插件. 想法最重要, 做一件事只要有了想法就成功了 80%.
我的 vim 配置仓库: hanleylee/dotvim
本文作者 Hanley Lee, 首发于 闪耀旅途, 如果对本文比较认可, 欢迎 Follow
联系客服