vim是一款支持插件、功能无比强大的编辑器,不管你的系统是linux、unix、mac仍是windows,都可以选择他来编辑文件或是进行工程级别 的coding。若是能把vim用好了,不只编程效率能获得大幅度提升,周围人也会所以而看得头晕眼花佩服不已,本身内心固然也会心花盛开啦。下面就让我 来介绍一下如何来进行配置。这些配置所涉及到的内容有:autocomplpop, ctags, TagList,omnicppcompletephp
首 先Vim是内建代码补全功能的,在不须要经过任何设置的状况下就能使用。在您编辑代码的时候,键入 ctrl+x, ctrl+o, ctrl+n, ctrl+p 等快捷键,就会弹出智能提示的菜单。可是这仍然不知足你们的要求。大多数IDE中,只要代码输入到相应的位置,补全提示就会自动的弹出来,而vim的这种 补全还须要本身手动的来触发。那么下面就介绍一种能够自动弹出补全提示的插件 — autocomplpophtml
== Autocomplpop ==python
首先,从http://www.vim.org/scripts/script.php?script_id=1879处 下载autocomplpop.vim文件(咱们所说的vim插件就是这样的*.vim格式的文件),而后将其放入vim文件目录下的plugin目录中 (unix/linux平台在/usr/share/vim/vim71中, windows平台在安装目录的vim71目录中),而后重启一下vim就会发如今编码时会自动弹出提示了。linux
细心的朋友会发现,光是利用 autocomplpop这个插件还远远达不到要求。好比说:在c++中使用.或是->访问对象或指针中的成员和函数时还没法自动弹出提示,另外, 即使是自动提示也只能提示咱们在当前文档中已输入的字符串。针对这种状况,咱们就须要安装ctags工具和OmniCppComplete插件。 ctags是用来对文件作标记的工具,OmniCppComplete是在c和c++语言范畴内,对上述智能补全的加强版。c++
== ctags ==git
ctags在http://ctags.sourceforge.net/下载源码,编译后安装。常规的标记命令为 ctags -R 。"-R"表示递归建立,也就包括源代码根目录下的全部子目录下的源程序。github
== CppCompleete ==编程
OmniCppComplete在http://www.vim.org/scripts/script.php?script_id=1520下载。下载 好以后根据里面的doc文档进行安装和使用。ubuntu
这样一来,代码补全就比较完善了。可是根据以往的经验,IDE中还有一个功能,那就是函数和变量的跳转查看。好比代码中出现
代码:
if(true){
doThis();
}vim
咱们想知道doThis()函数是如何定义和实现的,那么如何快速的来查看呢?咱们就须要安装Taglist插件
== Taglist ==
插件在http://vim.sourceforge.net/scripts/script.php?script_id=273下载。下载好以后,咱们能够根据其中的doc文档进行安装和配置。
咱们发现其实Taglist的使用也必需要依靠ctags所建立出来的tag文件。当tag文件造成、一切配置都配置完成以后。咱们能够ctrl+]来进行函数或者是变量跳转。好了,今后你就能够开始像IDE同样来使用vim进行编码了。
正文
可使用脚本/插件来给vim添加各类神奇的功能,从更换颜色主题、到代码智能提示,甚至项目管理。无数开发者经过开源社区贡献本身开发的插件,使得vim有可能变得无比强大。这儿http://vim-scripts.org/vim/scripts.html 是一份vim扩展脚本的列表。
然而,个人思想是尽可能不要使用vim插件,除了那些很是优秀且对本身的工做而言所必需的。这样,当须要配置一台新电脑或者临时要在别人的电脑上工做时,最起码能比较方便地配置好环境,或者直接使用默认环境熟练地完成任务,而不是离开了插件什么也不会。
对我本身而言,我基本上只须要4个(种)插件:
除了颜色主题因我的喜爱和环境不一样各不相同外,其他插件我都只会选择最流行,且公认最优、最好用的那个。下文将分别介绍这几种插件,并给出在Linux(Ubuntu, CentOS)和Mac OSX上配置的方法。可是在这以前,最好确认如下几个条件:
Vundle是一个流行的vim插件管理器,它的网址是https://github.com/VundleVim/Vundle.vim
如下是安装步骤:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 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插件、本地硬盘上的插件等。
Vundle正在自动安装在.vimrc中指定的插件:
vim默认有一些配色方案,若是这些都不喜欢,能够从网上下载安装别的配色方案。solarized和molokai都是流行的配色方案,然而这两个主题在终端(terminal)模式下或者SecureCRT上使用都会有一些问题,而我目前最喜欢的khaki没有这个问题,它的样子以下图所示(其中设置了行号、当前号高亮、语法高亮等)
安装步骤:
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
正文
可使用脚本/插件来给vim添加各类神奇的功能,从更换颜色主题、到代码智能提示,甚至项目管理。无数开发者经过开源社区贡献本身开发的插件,使得vim有可能变得无比强大。这儿http://vim-scripts.org/vim/scripts.html 是一份vim扩展脚本的列表。
然而,个人思想是尽可能不要使用vim插件,除了那些很是优秀且对本身的工做而言所必需的。这样,当须要配置一台新电脑或者临时要在别人的电脑上工做时,最起码能比较方便地配置好环境,或者直接使用默认环境熟练地完成任务,而不是离开了插件什么也不会。
对我本身而言,我基本上只须要4个(种)插件:
除了颜色主题因我的喜爱和环境不一样各不相同外,其他插件我都只会选择最流行,且公认最优、最好用的那个。下文将分别介绍这几种插件,并给出在Linux(Ubuntu, CentOS)和Mac OSX上配置的方法。可是在这以前,最好确认如下几个条件:
Vundle是一个流行的vim插件管理器,它的网址是https://github.com/VundleVim/Vundle.vim
如下是安装步骤:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 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插件、本地硬盘上的插件等。
Vundle正在自动安装在.vimrc中指定的插件:
vim默认有一些配色方案,若是这些都不喜欢,能够从网上下载安装别的配色方案。solarized和molokai都是流行的配色方案,然而这两个主题在终端(terminal)模式下或者SecureCRT上使用都会有一些问题,而我目前最喜欢的khaki没有这个问题,它的样子以下图所示(其中设置了行号、当前号高亮、语法高亮等)
安装步骤:
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