Vim 插件管理和配置说明

版权声明:本文为aggresss原创文章,未经博主允许不得转载。 作者:[email protected] https://blog.csdn.net/aggresss/article/details/79950252

  2006年开始使用 Vim ,那个年代的 Vim 还没有插件管理的概念,插件只能自己扔到 ~/.vim 或者 vimfiles 里面,插件的结构和 Unix 的文件风格很像,所以同一个插件的plugin, ftplugin, doc, syntax 等文件夹下都是混在一起,经常某个插件没安装好导致所有的 Vim 插件都罢工了。而且那时候总想试图把 Vim 变成一个全能 IDE ,各种插件各种配置不厌其烦的去添加和调试,折腾下来真的是浪费了很多时间,确实分散了自己的精力。
  经过这些年的沧海桑田,慢慢的开始理性的看待 Vim 的使用价值,我自己把它定位成一个战士的匕首,没有它是万万不行的,但我们的装备里还需要有步枪,一个技术上不断迭代更新的步枪。所以利用一个周末的时间,把自己的 Vim 配置和插件整理了一下,其实就是做了一个周末的减法,把 Vim 的配置变得尽量简单和可移植,把精简后的内容整理出来,纪念一下那个曾经通宵调试 Vim 插件的少年。
  现在 Vim 的插件管理工具已经有很多种了,并且越来越先进,因为比较在意稳定性所以使用 Vundle 作为插件管理。vim-scripts.org 这个组织很强大,把 https://www.vim.org/scripts/ 上的 Vim 插件都用 Git 的方式做了一遍镜像,也就是我们在 Github 上看到的 github.com/vim-scripts ,这样很多比较老的停止更新的插件就可以更容易被插件管理平台引用了。

首先需要安装 Vundle :

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

安装完成后需要修改 ~/.vimrc 来激活 Vundle , 我把整个配置文件都列了出来。这个配置文件的实用之处在于可移植性强,将 Vim 自己的配置信息和插件的配置信息分离。在配置文件加载初期使用 filereadable 判断是否存在插件配置文件,如果不存在就不激活 Vundle,这样如果我们在云端服务器上运行 Vim 时,在不需要插件的情况下只复制 .vimrc 文件,不用做任何修改就可以使用了,也就可以实现一个配置文件到处运行了。

vim配置文件 ~/.vimrc

" Vim config file composed by aggresss

set nocompatible    " required
filetype off        " required

" check if use bundle
if filereadable(expand("~/.vimrc.bundles"))
  " set the runtime path to include Vundle and initialize
  set rtp+=~/.vim/bundle/Vundle.vim
  call vundle#begin()
  Plugin 'VundleVim/Vundle.vim' " required
  Plugin 'taglist.vim'    " require install exuberant-ctags
  Plugin 'cscope.vim'     " require install cscope
  Plugin 'scrooloose/nerdtree'
  Plugin 'Valloric/YouCompleteMe'     " require compile

  call vundle#end()
  source ~/.vimrc.bundles    " source the plugin config
endif

filetype plugin indent on   " required

" editor style list
colorscheme desert
highlight Pmenu ctermfg=black ctermbg=darkcyan
highlight PmenuSel ctermfg=black ctermbg=gray
syntax on
set fileencodings=utf-8,gb2312,gbk,gb18030
set noswapfile
set number
set nowrap
set cursorline
set ts=4 sts=4 sw=4 et
set list
set listchars=tab:>-,trail:-
highlight WhitespaceEOL ctermbg=red guibg=red
match WhitespaceEOL /\s\+$/

" keyboard shortcut list
:map <F1>  :TlistToggle<cr>
:map <F2>  :NERDTreeToggle<cr>
:map <F3>  :Vexplore<cr>
:map <F4>  :Sexplore<cr>
:map <F5>  :Texplore<cr>
:map <F6>  :tabclose<cr>
:map <F7>  :bdelete<cr>
:map <F8>  :qall!<cr>
:map <F9>  :tabprevious<cr>
:map <F10> :tabfirst<cr>
:map <F11> :tablast<cr>
:map <F12> :tabnext<cr>
:map <C-l> :vertical res +4<cr>
:map <C-h> :vertical res -4<cr>
:map <C-j> :res +2<cr>
:map <C-k> :res -2<cr>

" custom command
command TrimCRLF %s/\r\(\n\)/\r/g
command TrimEOL %s/\s\+$//g

" EOF

下载方式:

wget https://raw.githubusercontent.com/aggresss/dotfiles/master/vim/.vimrc

配置文件修改完成后,打开 vim,输入 :BundleInstall 或者直接在终端输入 vim +BundleInstall +qall 安装插件

Vundle 的常用命令如下:

  • :PluginList 列举出列表中(.vimrc中)配置的所有插件
  • :PluginInstall 安装列表中全部插件
  • :PluginSearch 查找插件
  • :PluginClean 清除列表中没有的插件

Vundle 中安装的插件索引地址有三种类型:

  1. Github上 vim-scripts 的插件,只需要写出对应 Repo 的名字就可以,例如 taglist.vim
  2. Github上非 vim-scripts 的插件,需要写出 “Username/repo” ,例如:scrooloose/nerdtree
  3. 不在Github上的插件,需要写出Git或者文件全路径,例如:git://git.wincent.com/command-t.gitfile:///path/to/plugin

  Vim 的插件在严格上讲并不是开箱即用(Out of Box)的,有一些可以直接用、但有一些需要配置、还有一些需要依赖本地库或者可执行文件,最厉害的是还有一些需要自己编译。上面安装的几款插件中

  • scrooloose/nerdtree 是安装后就可以使用的;
  • taglist.vimcscope.vim 这两个插件如果在 Ubuntu 系统中需要使用 apt install exuberant-ctags cscope MacOS中 brew install ctags-exuberant cscope 命令安装相关的依赖后才能正常使用
  • Valloric/YouCompleteMe 则需要编译后才能使用,下面是编译步骤:
apt install build-essential cmake python-dev python3-dev
git clone https://github.com/Valloric/YouCompleteMe ~/.vim/bundle/
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
./install.py --clang-completer

插件下载和编译完成后,通过配置文件调整插件配置,下面是插件的配置文件 ~/.vimrc.bundles

" resource file for Vim bundle plugin

"VundleVim/Vundle.vim
let g:vundle_default_git_proto = 'git'

"taglist.vim
let g:Tlist_Show_One_File=1
let g:Tlist_Exit_OnlyWindow=1
let g:Tlist_WinWidth = 40
set tags=tags;/

"cscope.vim
let g:cscope_auto_update = 0

"scrooloose/nerdtree
let g:NERDTreeShowHidden=1
let g:NERDTreeWinSize=40

"Valloric/YouCompleteMe
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_seed_identifiers_with_syntax=1
let g:ycm_auto_trigger=1
let g:ycm_enable_diagnostic_signs=0
let g:ycm_enable_diagnostic_highlighting=0
let g:ycm_complete_in_string=0
let g:ycm_complete_in_comment=0
set completeopt=menu

" EOF

参考文档

  1. Ubuntu 搭建强大的 IDE —— Vim + Vundle + 插件(上)
  2. vim有哪些插件管理程序?都有些什么特点?
  3. How To Use Vundle to Manage Vim Plugins on a Linux VPS
  4. Using Modelines in Vim

猜你喜欢

转载自blog.csdn.net/aggresss/article/details/79950252