注:本文中所有的插件均为作者长时间实践挑选的产物,适合入门Vim编辑器之后的人群。通过增加不同插件,几乎可以完全抛弃鼠标,使代码效率倍增,给人一种很爽的感觉,哈哈哈。

以下所有配置均在ubuntu16.04下安装完成:

配置vim插件管理工具

常用的vim插件管理工具有两个,一个是vim-pathogen另一个是Vundle.vim.

配置vim-pathogen

对比了一下两个工具,这里我选择的是vim-pathogen,其配置过程简单描述如下,详细信息直接参考官网就可以了:

  • 首先在~/.vim下创建两个文件夹,一个是autoload,另一个是bundle,以后所有的插件应用都安装在bundle中。然后下载pathogen到autoload下面。

    $ mkdir -p ~/.vim/autoload ~/.vim/bundle && \
    $ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
    
  • 配置.vimrc,在.vimrc添加如下代码:

    $ execute pathogen#infect()
    $ syntax on
    $ filetype plugin indent on
    
  • 基本配置完上述内容就可以使用了,详情请参考文档。

配置vimogen

vimogen结合上面的pathogen使得安装插件更加容易,只要在一个固定的文件.vimogen_repos中添加上插件的github地址,然后就可以使用命令自动安装。在安装好pathogen之后,添加如下代码即可:

  • 安装

    $ git clone https://github/rkulla/vimogen
    $ chmod u+x vimogen
    $ cp vimogen to your $PATH
    
  • 使用
    首先将想要安装软件的地址放在~/.vimogen_repos里面(见我的github),像下面这样:

    https://github/Valloric/YouCompleteMe
    https://github/jeaye/color_coded
    https://github/rdnetto/YCM-Generator
    https://github/scrooloose/nerdtree
    https://github/Shougo/neocomplete.vim
    

    然后执行vimogen

    $ vimogen
    

    安装界面如下:

    1) INSTALL
    2) UNINSTALL
    3) UPDATE
    4) EXIT
    Enter the number of the menu option to perform:
    

    直接按1就可以了,程序会自动安装上面链接里面的插件,如果已经安装,则会跳过。
    如果按2,则是卸载已经安装的插件,直接选择要卸载的插件对应的编号即可。

    注: 在一些环境下,若不方便联网,可以放弃使用vimogen工具,直接在~/.vim/bundle路径下通过git clone将插件下载到本地即可。需要特别注意的是,如果在windows系统下载插件再上传到Linux系统,可能因为结束符不同而出现不兼容的现象。

配置常用插件

1. YouCompleteMe

YouCompleteMe基本上支持各种语言的自动补全,其安装过程需要编译一下,因此这里介绍一下安装过程,后面的几个插件因为直接使用vimogen装上就可以直接使用,因此不在过多介绍后面几类插件。YCM安装过程如下:

  • 下载
    直接使用vimogen一节介绍的方法讲YCM下载到~/.vim/bundle中,然后进入到~/.vim/bundle/YouCompleteMe/路径下。

  • 安装
    按照自己的需求选择编译命令,这里我因为常用的是python,但是也把c-family语言也选上了,因此执行下面命令进行编译安装:

    cd ~/.vim/bundle/YouCompleteMe
    python3 install.py --clang-completer
    # 注意:如果编译开始时出错一般是缺少各种工具,按照提示安装就可以了。
    
  • 配置
    这里根据自己的需求配置一些常用参数在.vimrc中。
    我配置的如下:

    "==================YCM====================
    let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
    nnoremap <F12> :YcmCompleter GoToDefinitionElseDeclaration<CR>
    

    配置的第二行配置了一个快捷键用来使用F12实现go to definition的功能。

    自动补全的效果如下:

常见问题:

  1. 在有一次配置服务器上的vim编辑器时候,遇到下面的问题:

    YouCompleteMe unavailable no module named builtins.

    查资料发现:这是因为网络很慢,导致要依赖的文件还没有下载全,所以可以先把文件下载了。

    解决办法

    $ cd /home/yourusername/.vim/bundle/YouCompleteMe
    $ git submodule update --init --recursive
    
  2. 无法下载libclang-12.0.0-x86_64-unknown-linux-gnu.tar.bz2,遇到的问题大概如下:

    Cannot find path to libclang in prebuilt binaries

    解决方法:手动下载所需文件放在报错的路径下即可,这里我找到的下载链接如下:
    链接:

    https://github/ycm-core/llvm/releases/tag/12.0.0

    路径

    ~/.vim/bundle/YouCompleteMe/third_party/ycmd/clang_archives

2. neocomplete.vim

  • neocomplete.vim实现路径的自动补全功能,效果如下:

3. nerdtree

  • nerdtree实现在左侧目录树功能,其效果如下:
  • 使用方法:
    1. m显示出菜单,然后根据菜单提示可以进行更多操作,比如这里我想新建一个文件(夹),直接按a, 然后输入想新建的文件(夹)即可。

4. vim-airline

主要负责状态栏显示

5. vim-gitgutter

主要负责git状态显示

6. vim-signify

和vim-gitgutter结合使用

7. vim-fugitive

更强大的git管理

8. auto-pairs

括号,引号等自动补全,很实用

9. indentLine

显示缩进

10. vim-colorschemes

代码配色方案

11. taglist.vim

Tag信息

12. vim-commentary

注释

13. a.vim

在.c和.h之间跳转

14 markdown-preview.nvim

markdown 预览

注意事项 这个插件安装时遇到个小插曲,安装完无法使用,具体解决方法见issue,链接如下:https://github/iamcco/markdown-preview.nvim/issues/45
解决方案:执行~/.vim/bundle/markdown-preview.nvim/app下面的install.sh安装所需插件即可。

最终效果

  • 通过配置上面插件,我经常使用的界面大概长成下面的样子。

.vimrc

最后把常用的.vimrc文件配置贴出来用于给读者参考. 感兴趣的读者可以去我的github上下载。

"============= pathogen ============
execute pathogen#infect()
syntax on
filetype plugin indent on

"=========== basic config ==========
set number              " 显示行号
set ruler               " 打开状态栏标尺
set shiftwidth=4        " 默认缩进4个空格 
set softtabstop=4       " 使用tab时 tab空格数 
set tabstop=4           " tab 代表4个空格  
set expandtab           " 使用空格替换tab
set hlsearch            " 高亮显示搜索结果
set t_Co=256            " 颜色
set showmatch           " 括号匹配
set foldmethod=indent   " 代码折叠
set foldlevel=99        " 代码折叠
set nofen               " 启动vim时打开所有折叠代码。
set history=50	        " keep 50 lines of command line history
set showcmd		        " display incomplete commands
set incsearch	        " do incremental searching
set noswapfile          " 不产生.swp文件
set modeline            " set modeline
set nocompatible        " Use Vim settings, rather then Vi settings
set wildmenu            " vim自身命令行模式智能补全
set ttimeout            " 设置ESC生效时间
set ttimeoutlen=100     " 设置ESC生效时间
syntax enable           " 开启语义分析

" 浅色高亮当前行
autocmd InsertEnter * se cul            
" 注释针对不同语言的注释方法
autocmd FileType cpp set commentstring=//\ %s
autocmd FileType php set commentstring=//\ %s
autocmd FileType python set commentstring=#\ %s

" 重新打开文档时光标回到文档关闭前的位置
if has("autocmd")
 autocmd BufReadPost *
 \ if line("'\"") > 0 && line ("'\"") <= line("$") |
 \ exe "normal g'\"" |
\ endif
endif

" 剪贴板复制粘贴,需安装vim-gtk
map <Leader>y "+y
map <Leader>p "+p

" 编译快捷键
autocmd filetype python nnoremap <F5> :w <bar> exec '!python '.shellescape('%')<CR>
autocmd filetype cpp nnoremap <F5> :w <bar> exec '!g++ --std=c++11 -pthread '.shellescape('%').' -o ./bin/'.shellescape('%:r').' && ./bin/'.shellescape('%:r')<CR>
autocmd filetype cc nnoremap <F5> :w <bar> exec '!g++ --std=c++11 -pthread '.shellescape('%').' -o ./bin/'.shellescape('%:r').' && ./bin/'.shellescape('%:r')<CR>
autocmd filetype dot nnoremap <F5> :w <bar> exec '!dot -Tsvg sqlparse.dot > sqlparse.svg'<CR>
autocmd Filetype java nnoremap <F5> :w <bar> exec '!javac '.shellescape('%'). ' -d ./bin'<CR>
autocmd filetype java nnoremap <F2> :w <bar> exec '!java -cp ./bin '.shellescape('%:r')<CR>

"新建.c,.h,.sh,.Java, .python文件,自动插入文件头
autocmd BufNewFile *.py,*.cpp,*,*.[ch],*.sh,*.Java,*.go exec ":call SetTitle()"
func SetTitle()
    if &filetype == 'sh' || &filetype == 'python'
        call setline(1,"\#########################################################################")
        call append(line("."),   "\# File Name:    ".expand("%"))
        call append(line(".")+1, "\# Author:       xuyangcao")
        call append(line(".")+2, "\# Mail:         caoxuyang@bjtu.edu")
        call append(line(".")+3, "\# Created Time: ".strftime("%c"))
        call append(line(".")+4, "\#########################################################################")
        call append(line(".")+5, "\#!/bin/bash")
        call append(line(".")+6, "")
    else
        call setline(1, "/*************************************************************************")
        call append(line("."),   "> File Name:     ".expand("%"))
        call append(line(".")+1, "> Author:        xuyangcao")
        call append(line(".")+2, "> Mail:          caoxuyang@bjtu.edu")
        call append(line(".")+3, "> Created Time:  ".strftime("%c"))
        call append(line(".")+4, "> Description:   ")
        call append(line(".")+5, " ************************************************************************/")
        call append(line(".")+6, "")
    endif
endfunc
autocmd BufNewFile * normal G     "新建文件后,自动定位到文件末尾


"=============== YCM ================
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
nnoremap <F12> :YcmCompleter GoToDefinitionElseDeclaration<CR>

"============== nerdtree ============
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif "当没指定文件时nerdtree自动打开
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif "当只剩下nerdtree时候vim自动退出
map <C-n> :NERDTreeToggle<CR> 
" 使用NERDTree插件查看工程文件。设置快捷键
nnoremap <silent> <Leader>n  :NERDTreeToggle <CR> 
" 设置NERDTree子窗口位置
let NERDTreeWinPos="left"
" 设置忽略的文件
let NERDTreeIgnore=['\.vim$', '\~$', '\.o$', '\.d$', '\.a$', '\.out$', '\.tgz$']

"============= ultisnips ============
""Trigger configuration. Do not use <tab> if you use
"https://github/Valloric/YouCompleteMe.
let g:UltiSnipsExpandTrigger="<c-o>"
let g:UltiSnipsJumpForwardTrigger="<c-j>"
let g:UltiSnipsJumpBackwardTrigger="<c-k>"

"========== ctags & taglist ==========
nmap<leader>tg :!ctags -R --fields=+aS --extra=+q<CR>
nnoremap  <C-t>  :TlistToggle <CR> 
nnoremap  <Leader>u  :TlistUpdate <CR> 
let Tlist_Inc_Winwidth=0            "禁止自动改变当前Vim窗口的大小
let Tlist_Use_Right_Window=1        "把方法列表放在屏幕的右侧
let Tlist_File_Fold_Auto_Close=1    "让当前不被编辑的文件的方法列表自动折叠起来

"============ colorscheme ============
colorscheme molokai

"========== airlinae theme =========== 
let g:airline_theme='molokai'

"================= A =================
nmap <Leader>a :A<CR>         " 快速切换C H源文件(a.vim)

"========= Markdown Preview ========== 
nmap <F8> <Plug>MarkdownPreview
nmap <F9> <Plug>MarkdownPreviewStop

# .vimogen_repos

https://github/Valloric/YouCompleteMe
https://github/SirVer/ultisnips.git
https://github/scrooloose/nerdtree
https://github/Shougo/neocomplete.vim

https://github/airblade/vim-gitgutter
https://github/tpope/vim-fugitive

https://github/Yggdroot/indentLine
https://github/tpope/vim-commentary.git
https://github/jiangmiao/auto-pairs

https://github/vim-scripts/taglist.vim.git
https://github/vim-scripts/a.vim.git

https://github/vim-airline/vim-airline
https://github/vim-airline/vim-airline-themes
https://github/flazz/vim-colorschemes  

https://github/iamcco/markdown-preview.nvim.git

参考资料

  • Building-Vim-from-source
  • [ubuntu]自己编译安装vim 8.0的方法
  • github: vim-pathogen
  • github: Vundle.vim
  • YouCompleteMe A code-completion engine for Vim
  • 跟我一起学习VIM - vim插件合集
  • vim查找/替换字符串
  • Vim技巧之分割窗口
  • vim 多窗口编辑
  • VIM选择文本块/复制/粘贴
  • ultisnips
  • vimtex
  • Auto-call auto-completion v2
  • anyone_else_abusing_ultisnips?
  • vim-airline
  • dotfiles
  • vim-latex-live-preview
  • fugitive-vim-working-with-the-git-index
  • vimcasts
  • vim的powerline插件没有颜色
  • Creating a new file or directory in Vim using NERDTree
  • Using Spell Checking in Vim
  • Vim 实用插件推荐(2017)
  • Tmux使用手册
  • YouCompleteMe(YCM)安装时遇到的问题(能遇到的几乎都遇到了)
  • https://github/iamcco/markdown-preview.nvim/issues/171

这里是引用

更多推荐

学习vim: 配置python和c++相关插件