[toc]
背景
在终端下使用 vim 进行编辑时,默认情况下编辑的界面上是没有显示行号、语法高亮度显示、智能缩进等功能。为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc。
用户在自己的目录下修改 .vimrc 的话,修改内容只对本用户有效。要想全部用户有效,需要修改/etc/vim/vimrc。Vim存在多个配置文件vimrc,比如/etc/vimrc,此文件影响整个系统的Vim。还有/.vimrc,r此文件只影响本用户的Vim。而且/.vimrc文件中的配置会覆盖/etc/vimrc中的配置。这里我们只修改~/.vimrc文件。
vim的插件(plugin)安装在vim的run time path目录下,你可以在Vim命令行下运行"set rtp“命令查看。这里我们选择安装在~/.vim目录,没有就创建一个。
安装插件管理器 Vundle
1 | git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim |
安装一个插件
- 打开 vim 输入 :PluginInstall 。
- :PluginInstall 命令会安装在 .vimrc 文件中列出来的所有插件。
- :PluginInstall
安装指定的插件。
清理未用插件
- 打开 vim 输入 :PluginClean 。
配置插件
ctrlp
ctrlp - Vim的模糊搜索工具,支持文件,缓冲区,MRU(Most Recently Used)文件和标签等的搜索,也支持通过正则表达式搜索(Ctrl-r进行切换)。 Vim插件之ctrlp
Taglist
Taglist是vim的一个插件,提供源代码符号的结构化视图。
cscope
在Vim中,通过cscope的查询,跳转到指定的地方就像跳转到任何标签;她能够保存标签栈,所以通过合适的键盘映射绑定,你能够在函数向后或向前跳转,就像通常使用的tags一样。
a.vim
实现源文件和头文件之间的切换。
1 | # Configuration in vundle |
rtag
Q: CMake Error at src/CMakeLists.txt:103 (message): Failed to compile small clang test app.
It's likely that the include file <clang-c/Index.h> could not be found!
Maybe you need to install the clang development package (delete the CMakeCache.txt file before trying to run cmake again after installation)?
See CMakeFiles/CMakeError.log for more info.
A: sudo apt install llvm-7-dev libclang-7-dev
- delete the CMakeCache.txt file
YouCompleteMe
YouCompleteMe:一个随键而全的、支持模糊搜索的、高速补全的插件。由 google 公司搜索项目组的软件工程师 Strahinja Val Markovic 所开发,YCM 后端调用 libclang (以获取 AST,当然还有其他语言的语义分析库)、前端由 C++ 开发(以提升补全效率)、外层由 python 封装(以成为 vim 插件)。插件安装非常复杂。
Q: The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). YCM core library not detected; you need to compile YCM before using it. Follow the instructions in the documentation.
- A:
1
2cd ~/.vim/bundle/YouCompleteMe
python3 install.py --allor
1
2
3
4# Compiling YCM with semantic support for C-family languages through clangd:
cd ~/.vim/bundle/YouCompleteMe
./install.py --clangd-completer
Q: NoExtraConfDetected: No .ycm_extra_conf.py file detected, so no compile flags are available. Thus no semantic support for C/C++/ObjC/ObjC++. Go READ THE DOCS NOW, DON'T file a bug report.
- A:
1
2cd ~/.vim/bundle/YouCompleteMe
sudo bash ./install.sh
- A:
- Q: CMake Error: The source directory
"/home/waylon/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/cregex"
does not appear to contain CMakeLists.txt. Specify --help for usage, or
press the help button on the CMake GUI. ERROR: the build failed.
NOTE: it is highly unlikely that this is a bug but rather that this is a problem with the configuration of your system or a missing dependency.A:
1
2
3
4cd ~/.vim/bundle/YouCompleteMe
rm -rf YouCompleteMe/third_party/ycmd/third_party/cregex
cd ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party
git submodule update --init --recursive当使用 git clone 下载的工程中带有 submodule 时,初始的时候,submodule 的内容并不会自动下载下来的,此时,只需执行如下命令:
即可将子模块内容下载下来后工程才不会缺少相应的文件。1
git submodule update --init --recursive
TagHighlight
TagHighlight: Extra highlighting of typedefs, enumerations etc (based on ctags), 可以让typedef的类型,还有枚举,宏等都高亮。
ctags
产生标记文件以帮助在源文件中定位对象。
在Vim中使用ctags
LeaderF
一款模糊查找插件——LeaderF,无论是从性能还是匹配精度上,都远远超越ctrlp。LeaderF是一个用Python写的vim插件,可以在成千上万数十万个文件中,通过模糊查找的方式,快速找到目标文件。它还有很多衍生功能:快速打开或定位某个buffer、最近使用的文件(mru)、tags(包括函数、类、变量等)、命令历史、文件中的某一行、vim的help、marks等等。
让人相见恨晚的vim插件:模糊查找神器LeaderF
Tagbar
Vim tag
tag 是什么?一个位置,它记录了关于一个标识符在哪里被定义的信息,比如 C 或 C++ 程序中的一个函数定义。这种 tag 聚集在一起被放入一个tags文件。这个文件可以让Vim能够从任何位置起跳达到tag所指示的位置-标识符被定义的位置。
对于程序来说,Tag文件中保存了诸如函数、类、结构、宏等的名字,它们所处的文件,以及如何通过Ex命令跳转到这些标签。它是一个纯文本文件,因此你可以手工的编辑它,也可以使用脚本对其进行操作。通常我们使用名为ctags的程序来生成这样的tag文件。vim能直接使用ctags程序所生成的tag文件。在UNIX系统下的ctags功能比较少,所以一般我们使用Exuberant Ctags (在大多数 Linux系统上,它是缺省的ctags程序),它能够支持多达33种程序语言,足以满足我们开发的需要了。
tags 文件的产出最简单的方法是在需要生成tags的工程项目的根目录下执行ctags -R命令, 这会调用tags递归的扫描当前目录以及所有子目录中可以被tags识别的文件所以文件数据信息都会汇集到tags文件中。
Tagbar 依赖 ctags,所以需要先安装 ctags。
1 | sudo apt-get install ctags |
- Q: 打开 vim 时提示,“ Please check your g:tagbar_ctags_bin setting.
”
- A:
1
sudo apt install ctags
- A:
- Q: Tagbar: Exuberant ctags not found at '/usr/bin/ctags'! Please check your g:tagbar_ctags_bin setting.
- A: sudo apt install exuberant-ctags
- exuberant-ctags: build tag file indexes of source code definitions
NERD-tree
NERDTree的作用就是列出当前路径的目录树,一般IDE都是有的。可以方便的浏览项目的总体的目录结构和创建删除重命名文件或文件名。 NERDTree插件
- :NERDTreeFind 找到当前文件的目录
1 | # Ctrl + F to find the file's directory |
vim 配置
VIM
: What is the difference between let g:, let b:
:set is for setting options, :let for assigning a value to a variable.
ps. vim专用
设置快捷键
.vimrc 文件配置
1 | # 文件编码 |