C++ code completion

本文介绍如何在Vim编辑器中配置并使用C++代码自动补全功能。通过安装OmniCppComplete插件及设置ctags,可以为C++文件提供丰富的代码补全建议。此外,还介绍了如何针对不同库文件创建标签文件,以及如何在.vimrc文件中进行必要的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This tip describes how to use C++ code completion in Vim (omni completion).

When editing a C++ file, enter the command :set omnifunc? to see what completion function is currently used. If the result is omnifunc=ccomplete#Complete, it means you are using the C completion provided with Vim (not C++). If your :set omnifunc? is not set as desired or maybe empty this is a good workaround for C++ files:

au BufNewFile,BufRead,BufEnter *.cpp,*.hpp set omnifunc=omni#cpp#complete#Main


The following procedure provides C++ completion.

Contents

[show]

Required setup

1. Install OmniCppComplete. See its doc/omnicppcomplete.txt file for information.

2. Make a directory, for example ~/.vim/tags that will hold your ctags.

3. Create stdc++ tags: Download and unpack the modified libstdc++ headers to ~/.vim/tags/cpp_src

4. Run ctags:

$ cd ~/.vim/tags
$ ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f cpp cpp_src

5. Add additional tags (change to your system/likings):

$ ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f gl /usr/include/GL/   # for OpenGL
$ ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f sdl /usr/include/SDL/ # for SDL
$ ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f qt4 /usr/include/qt4/ # for QT4

6. Edit your ~/.vimrc and change to your system/likings:

" configure tags - add additional tags here or comment out not-used ones
set tags+=~/.vim/tags/cpp
set tags+=~/.vim/tags/gl
set tags+=~/.vim/tags/sdl
set tags+=~/.vim/tags/qt4
" build tags of your own project with Ctrl-F12
map <C-F12> :!ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q .<CR>

" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1
let OmniCpp_ShowPrototypeInAbbr = 1 " show function parameters
let OmniCpp_MayCompleteDot = 1 " autocomplete after .
let OmniCpp_MayCompleteArrow = 1 " autocomplete after ->
let OmniCpp_MayCompleteScope = 1 " autocomplete after ::
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
" automatically open and close the popup menu / preview window
au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
set completeopt=menuone,menu,longest,preview

Using omni completion

When everything is set up properly you can use the code completion. Make sure the 'filetype' of your file is set to cpp. You can check this by doing :set filetype?. If your 'filetype' isn't cpp, you might have to enable file type detection by adding filetype on to your vimrc, and restart Vim.

Now start typing (for me this only works if std:: is indented; otherwise I get Omni-completion (^0^N^P) Pattern not found):

std::

A box opens with suggestions. You can use <C-N> and <C-P> to navigate. <C-X><C-O> opens the omnicompletion manually. A preview window should also appear on the top which shows the signature of functions and the like. Choose vector and continue:

std::vector<int> vi;
vi.

Another box opens with suggestions. Choose push_back( and continue.

As you can see this is a really nice feature which is especially handy when you use an external library which you don't know very well. Simply create the tags as described above and browse through the suggestions.

Additional information

It is nice to have the headers of the used libs really on your system so the preview window has something to show. So don't delete the headers after creating the tag files.

References

Comments

If you want completion to work for local variables in your code, you have to add the '+l' (letter "l" from local) option to the c++-kinds argument, as in:

map <C-F12> :!ctags -R --c++-kinds=+pl --fields=+iaS --extra=+q .<CR>

Some recent edits have introduced --sort=1 (the "1" should be "yes") and --sort=yes. I believe ctags defaults to sort=yes so these edits are not necessary. I'm wondering if the edits are useful. JohnBeckett23:32, May 28, 2010 (UTC)

The autocompletion does *NOT* work for boost shared pointers. So for example, if there is a section of code that looks like:

boost::shared_ptr<const ObjectType> someObject=boost::shared_ptr<const ObjectType>(new ObjectType());
int member = someObject->getMember();

The getMember method will not show up in the dropdown list, because ctags is enabled for ObjectType, not when it's wrapped inside a boost shared pointer. If anyone knows a work around for this, it would be very useful for those C++ programmers who work with Boost.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值