可使用腳本/插件來給vim添加各類神奇的功能,從更換顏色主題、到代碼智能提示,甚至項目管理。無數開發者經過開源社區貢獻本身開發的插件,使得vim有可能變得無比強大。這兒http://vim-scripts.org/vim/scripts.html 是一份vim擴展腳本的列表。php
然而,個人思想是儘可能不要使用vim插件,除了那些很是優秀且對本身的工做而言所必需的。這樣,當須要配置一臺新電腦或者臨時要在別人的電腦上工做時,最起碼能比較方便地配置好環境,或者直接使用默認環境熟練地完成任務,而不是離開了插件什麼也不會。html
對我本身而言,我基本上只須要4個(種)插件:python
除了顏色主題因我的喜愛和環境不一樣各不相同外,其他插件我都只會選擇最流行,且公認最優、最好用的那個。下文將分別介紹這幾種插件,並給出在Linux(Ubuntu, CentOS)和Mac OSX上配置的方法。可是在這以前,最好確認如下幾個條件:c++
Vundle是一個流行的vim插件管理器,它的網址是https://github.com/VundleVim/Vundle.vimgit
如下是安裝步驟:github
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Vundle set nocompatible " be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim' " The following are examples of different formats supported. " Keep Plugin commands between vundle#begin/end. " plugin on GitHub repo "Plugin 'tpope/vim-fugitive' " plugin from http://vim-scripts.org/vim/scripts.html "Plugin 'L9' " Git plugin not hosted on GitHub "Plugin 'git://git.wincent.com/command-t.git' " git repos on your local machine (i.e. when working on your own plugin) "Plugin 'file:///home/gmarik/path/to/plugin' " The sparkup vim script is in a subdirectory of this repo called vim. " Pass the path to set the runtimepath properly. "Plugin 'rstacruz/sparkup', {'rtp': 'vim/'} " Avoid a name conflict with L9 "Plugin 'user/L9', {'name': 'newL9'} " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " To ignore plugin indent changes, instead use: "filetype plugin on " " Brief help " :PluginList - lists configured plugins " :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate " :PluginSearch foo - searches for foo; append `!` to refresh local cache " :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal " " see :h vundle for more details or wiki for FAQ " Put your non-Plugin stuff after this line """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
從其中的註釋能夠知道,Vundle支持多種形式的插件源,並給出了示例。這些插件源包括:github上的插件、http://vim-scripts.org/vim/scripts.html上的插件、非github上的git插件、本地硬盤上的插件等。ubuntu
Vundle正在自動安裝在.vimrc中指定的插件:vim
vim默認有一些配色方案,若是這些都不喜歡,能夠從網上下載安裝別的配色方案。solarized和molokai都是流行的配色方案,然而這兩個主題在終端(terminal)模式下或者SecureCRT上使用都會有一些問題,而我目前最喜歡的khaki沒有這個問題,它的樣子以下圖所示(其中設置了行號、當前號高亮、語法高亮等)bash
安裝步驟:數據結構
if !has("gui_running") set t_Co=256 endif colorscheme khaki
保存後重啓vim便可。
爲本身的代碼寫好註釋是一個良好的習慣,而編寫Doxygen風格的註釋更是能夠經過doxygen工具爲代碼本身生成文檔,很是好用。DoxygenToolkit(https://github.com/vim-scripts/DoxygenToolkit.vim)就是這樣的一個插件。安裝和使用:
let g:DoxygenToolkit_authorName="zzq@moon.net"
/** * @file test.cpp * @brief * @author zzp@moon.net * @version 1.0 * @date 2015-08-21 */
並把光標停留在@brief 後面,等待輸入文件描述。
在光標定位到數據結構聲明或函數聲明的第一行,運行:Dox,將生成數據結構或函數的註釋骨架,以下:
/** * @brief */ struct foo { char str; void* ptr; }; /** * @brief * * @param a * @param b * * @return */ int bar(int a, int b) { return a+b; }
並把光標定位在@brief 後,期待你輸入具體的註釋內容。
寫代碼的時候,變量名、函數名、類名等代碼符號的智能提示和補全功能是很是有用的,能夠大大提升編碼效率。然而,在YouCompleteMe(簡稱YCM)這個神奇的插件出現以前,vim一直使用tag機制來完成這個功能。因爲tag只會笨拙地分析代碼中的字符串,並不能識別其語法說語義,致使代碼的提示並很差用。隨着clang的出現,使開發人員能夠對程序代碼進行事實上的語義分析(調用clang分析器之類的),因而真正的智能提示和補全插件出現了,它就是由 google 的工程師 Strahinja Val Markovic 所開發的YCM(https://github.com/Valloric/YouCompleteMe)。
YCM使用C++和python開發,是一個複雜的插件,光是經過Vundle下載的文件就達到120多MB。另外YCM不僅是有新的開發的功能,它還包含了其餘一些有用的插件。下圖是做者本人提示的演示動圖:
除了代碼提示與補全外,藉助libclang強大的語法與語義分析能力,YCM還能夠在編輯的時候提示出錯的行與出錯緣由,以下圖所示:
另外,YCM還能夠補全路徑,文件名等。
安裝與配置步驟:
sudo apt-get install vim-addon-manager sudo apt-get install vim-youcompleteme vim-addons install youcompleteme
1.1.下載Vundle和YouCompleteMe插件
輸入如下指令,下載Vundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
下載成功後,在用戶根目錄下面,修改.vimrc文件,追加下面語句以便後續安裝YouCompleteMe插件
set nocompatible filetype off set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'gmarik/Vundle.vim' Plugin 'Valloric/YouCompleteMe' call vundle#end() filetype plugin indent on
而後在vim中先按Esc建,而且輸入如下指令安裝插件:
:PluginInstall
1.2.編譯YouCompleteMe
在編譯以前下載編譯工具,準備編譯YouCompleteMe
yum install gcc gcc-c++ cmake python-devel
編譯YouCompleteMe使其支持C/C++ 自動補全
cd ~/.vim/bundle/YouCompleteMe ./install.py --clang-completer
# These are the compilation flags that will be used in case there's no # compilation database set (by default, one is not set). # CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR. flags = [ '-Wall', '-Wextra', '-Werror', '-Wc++98-compat', '-Wno-long-long', '-Wno-variadic-macros', '-fexceptions', '-DNDEBUG', # You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM # source code needs it. '-DUSE_CLANG_COMPLETER', # THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which # language to use when compiling headers. So it will guess. Badly. So C++ # headers will be compiled as C headers. You don't want that so ALWAYS specify # a "-std=<something>". # For a C project, you would set this to something like 'c99' instead of # 'c++11'. '-std=c++11', # ...and the same thing goes for the magic -x option which specifies the # language that the files to be compiled are written in. This is mostly # relevant for c++ headers. # For a C project, you would set this to 'c' instead of 'c++'. '-x', 'c++', '-isystem', '../BoostParts', '-isystem', # This path will only work on OS X, but extra paths that don't exist are not # harmful '/System/Library/Frameworks/Python.framework/Headers', '-isystem', '../llvm/include', '-isystem', '../llvm/tools/clang/include', '-I', '.', '-I', './ClangCompleter', '-isystem', './tests/gmock/gtest', '-isystem', './tests/gmock/gtest/include', '-isystem', './tests/gmock', '-isystem', './tests/gmock/include', ]
b. 在.vimrc中添加如下配置項(更多項見https://github.com/Valloric/YouCompleteMe#general-usage)。
" YCM " 容許自動加載.ycm_extra_conf.py,再也不提示 let g:ycm_confirm_extra_conf=0 " 補全功能在註釋中一樣有效 let g:ycm_complete_in_comments=1 " 開啓tags補全引擎 let g:ycm_collect_identifiers_from_tags_files=1 " 鍵入第一個字符時就開始列出匹配項 let g:ycm_min_num_of_chars_for_completion=1 " YCM相關快捷鍵,分別是\gl, \gf, \gg nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR> nnoremap <leader>gf :YcmCompleter GoToDefinition<CR> nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
其中,
--fields=+l
option (that's a lowercase L
, not a one) because YCM needs the language:<lang>
field in the tags output。若有問題請參閱YCM官網相關FAQ;" 引入 C++ 標準庫tags set tags+=/data/misc/vim/stdcpp.tags
其餘技巧:
如5.1所述,YouCompleteMe插件是如此地強大。然而,某些時候它可能仍是會有些小問題,好比沒法提示宏定義等等,致使沒法補全,這時候仍是須要藉助傳統的tags文件。indexer插件能夠針對不一樣的工程目錄自動地生成、更新和引入不一樣的tags文件,詳見http://www.vim.org/scripts/script.php?script_id=3221。它須要依賴DfrankUtil和Vimprj 兩個插件,須要一併安裝。
安裝與配置步驟:
1. 在.vimrc的Vundle區域內加入如下內容
Plugin 'DfrankUtil' Plugin 'vimprj' Plugin 'indexer.tar.gz'
以後運行:PluginInstall安裝;
2. 打開.vimrc,加入如下內容:
" indexer " 設置indexer 調用 ctags 的參數 " 默認 --c++-kinds=+p+l,從新設置爲 --c++-kinds=+p+l+x+c+d+e+f+g+m+n+s+t+u+v " 默認 --fields=+iaS 不知足 YCM 要求,需改成 --fields=+iaSl let g:indexer_ctagsCommandLineOptions="--c++-kinds=+p+l+x+c+d+e+f+g+m+n+s+t+u+v --fields=+iaSl --e xtra=+q"
3. indexer會根據你的代碼工程的不一樣,自動生成並在其中的代碼文件被打開時自動加載tags文件。它是經過配置文件來指定工程設置的,此文件爲~/.indexer_files。如下是一個配置示例,演示了2個不一樣的工程的Indexer配置。
[CoolProject] /home/user/cool_project [AnotherProject] option:ctags_params = "--languages=c++" /home/user/another_project/src /home/user/another_project/lib