vim 插件配置

[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 目录,没有就创建一个。

vim 打开中文乱码

  • 用 vim 打开 .vimrc 配置文件
    ‘’’
    vim ~/.vimrc
    ‘’’
  • 在.vimrc 中加入如下内容
    ‘’’
    set termencoding=utf-8
    set encoding=utf8
    set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030
    ‘’’
    说明:encoding 是 Vim 内部使用的字符编码方式,一般设置为 utf8。termencoding 是 Vim 所工作的终端 (或者 Windows 的 Console 窗口) 的字符编码方式,一般设置为 utf8。fileencodings 是 Vim 自动探测 fileencoding 的顺序列表,启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的字符编码方式。这些设置需要 set 命令来进行设置生效。
  • 执行 :wq 保存退出 vim,再次用 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
2
3
4
# Configuration in vundle
Plugin 'vim-scripts/a.vim'
#
nnoremap <leader>a :A<CR>

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 插件)。插件安装非常复杂。

  • 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
    2
    cd ~/.vim/bundle/YouCompleteMe
    python3 install.py --all

    or

    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
2
cd ~/.vim/bundle/YouCompleteMe
sudo bash ./install.sh
  • 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
    4
    cd ~/.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
  • 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 都是有的。可以方便的浏览项目的总体的目录结构和创建删除重命名文件或文件名。

1
2
3
# Ctrl + F to find the file's directory
# Configure in ~/.vimrc .
map <C-f> :NERDTreeFind<CR>

设置快捷键

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 文件编码
set encoding=utf-8
# 显示行号
set number
# 取消换行
set nowrap
# 显示光标当前位置
set ruler
# 设置缩进
set cindent
set tabstop=2
set shiftwidth=2
# 突出显示当前行
set cursorline
# 查找
set ic
set hls
set is
# 左下角显示当前 vim 模式
set showmode
# 代码折叠
set nofoldenable
主题
syntax enable
set background=dark
colorscheme solarized

vim 使用技巧_快捷键

References