之前我一直有一个问题苦恼着我,就是在C++之中如果有关s
truct变量的时候,如果我想像C一样地利用CTAGS之类的工具在跳转之类的操作就很吃力了。不过我一直用一个小技巧。直到今天我发现了ominicppcomplete这个插件,让我欣喜若狂啊。下面介绍介绍。
其中一则英文文档说得很详细。
看看吧。
C++ autocompletion is possible with VIM.
This is officially the coolest tech discovery I’ve made this year.
And it’s easy to setup.
Here’s how to configure C++ IntelliSense for Vim, with an example that demonstrates how to enable code completion for the the C++ Standard Template Library (STL).
1. Download
You need the following:
Vim
Exuberant Ctags
OmniCppComplete
2. Install
With Ubuntu 8.10 you can install Vim and Exuberant Ctags like so:
sudo apt-get install vim exuberant-ctags
Install OmniCppComplete by downloading the plugin and extracting it to your Vim settings (~/.vim/) folder.
Then open Vim, and type the following:
:helptags $HOME/.vim/doc
3. Configure Vim
Add the following to your .vimrc file (you only need the required and ctags sections, but I included options I found useful – hack as you please):
" --- OmniCppComplete ---
" -- required --
set nocp " non vi compatible mode
filetype plugin on " enable plugins
" -- optional --
" auto close options when exiting insert mode
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
set completeopt=menu,menuone
" -- configs --
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype (i.e. parameters) in popup window
" -- ctags --
" map +F12 to generate ctags for current folder:
map :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
" add current directory's generated tags file to available tags
set tags+=./tags
4. Generate Ctags
To generate the ctags and enable code completion for the code you are currently working on (everything in your current working directory and its sub-directories), you can simply hit CTRL+F12.
To understand how this works, check out the key-binding you enabled in your .vimrc file in step 3 of this tutorial.
To enable code completion for an external library, you need to:
Do pre-processing on the library (if needed).
Generate a ctags file for the library.
Tell Vim where to find the ctags file.
I will demonstrate using the Standard Template Library (STL) as an example.
Start by downloading the STL source and extracting it somewhere meaningful (e.g. /usr/local/lib/stl3.3).
For most libraries you can skip the first step, but some libraries (like the STL) will need a little pre-processing because of the complexity introduced by macros.
Change to the directory where you extracted the STL source, and run the following:
$ find . -name '*.h' | xargs sed -i 's/__STL_BEGIN_NAMESPACE/namespace std {/'
$ find . -name '*.h' | xargs sed -i 's/__STL_END_NAMESPACE/}/'
Now we’re ready to generate the ctags. In the directory where you extracted the STL source, run:
$ ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ./
This will create a file called ‘tags’ in the current directory. Rename and move the file somewhere meaningful, e.g.:
$ mkdir ~/.myTags
$ mv tags ~/.myTags/std3.3.tags
Lastly, tell Vim where to find the tags by adding the following to your .vimrc file:
set tags+=~/.myTags/std3.3.tags
And viola.
感觉用起来很爽,不过后来我接着再装 了个插件,让它们之间的连接更加自然,更加高效。它就是:
AutoComplPop : Automatically opens popup menu for completions
下载地址:
阅读(3052) | 评论(1) | 转发(0) |