Code completion (IntelliSense) for C++ in Vim with OmniCppComplete

转自:http://design.liberta.co.za/articles/code-completion-intellisense-for-cpp-in-vim-with-omnicppcomplete/

 

C++ autocompletion is possible with VIM.

Vim OmniCppComplete autocomplete example 2

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:

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 <ctrl>+F12 to generate ctags for current folder:
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR>
" 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 voila.

Vim OmniCppComplete autocomplete example 3
Vim OmniCppComplete autocomplete example 1
Vim OmniCppComplete autocomplete example 2

Tags: autocompletion , C++ , code completion , IntelliSense , Linux , omnicppcomplete , UNIX , vim

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值