Wikia

Vim Tips Wiki

Changes: C++ code completion

Edit

Back to page

m (Reverted edits by 24.9.163.244 (talk | block) to last version by 173.20.48.124)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 13: Line 13:
 
This tip describes how to use C++ code completion in Vim (omni completion).
 
This tip describes how to use C++ code completion in Vim (omni completion).
   
When editing a C++ file, enter the command <tt>:set omnifunc?</tt> to see what completion function is currently used. If the result is <tt>omnifunc=ccomplete#Complete</tt>, it means you are using the C completion provided with Vim (not C++). If your <tt>:set omnifunc?</tt> is not set as desired or maybe empty this is a good workaround for C++ files:
+
When editing a C++ file, enter the command <code>:set omnifunc?</code> to see what completion function is currently used. If the result is <code>omnifunc=ccomplete#Complete</code>, it means you are using the C completion provided with Vim (not C++). If your <code>:set omnifunc?</code> is not set as desired or maybe empty this is a good workaround for C++ files:
   
 
<pre>
 
<pre>
Line 23: Line 23:
   
 
==Required setup==
 
==Required setup==
1. Install {{script|id=1520|text=OmniCppComplete}}. See its <tt>doc/omnicppcomplete.txt</tt> file for information.
+
1. Install {{script|id=1520|text=OmniCppComplete}}. See its <code>doc/omnicppcomplete.txt</code> file for information.
   
2. Make a directory, for example <tt>~/.vim/tags</tt> that will hold your ctags.
+
2. Make a directory, for example <code>~/.vim/tags</code> that will hold your ctags.
   
3. Create stdc++ tags: Download and unpack the {{script|id=2358|text=modified libstdc++ headers}} to <tt>~/.vim/tags/cpp_src</tt>
+
3. Create stdc++ tags: Download and unpack the {{script|id=2358|text=modified libstdc++ headers}} to <code>~/.vim/tags/cpp_src</code>
   
 
4. Run ctags:
 
4. Run ctags:
Line 68: Line 68:
   
 
==Using omni completion==
 
==Using omni completion==
When everything is set up properly you can use the code completion. Make sure the <tt>'filetype'</tt> of your file is set to <tt>cpp</tt>. You can check this by doing <tt>:set filetype?</tt>. If your <tt>'filetype'</tt> isn't <tt>cpp</tt>, you might have to enable file type detection by adding <tt>filetype on</tt> to your [[vimrc]], and restart Vim.
+
When everything is set up properly you can use the code completion. Make sure the <code>'filetype'</code> of your file is set to <code>cpp</code>. You can check this by doing <code>:set filetype?</code>. If your <code>'filetype'</code> isn't <code>cpp</code>, you might have to enable file type detection by adding <code>filetype on</code> 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):
 
Now start typing (for me this only works if std:: is indented; otherwise I get Omni-completion (^0^N^P) Pattern not found):
Line 74: Line 74:
 
std::
 
std::
 
</pre>
 
</pre>
A box opens with suggestions. You can use <tt>&lt;C-N&gt;</tt> and <tt>&lt;C-P&gt;</tt> to navigate. <tt>&lt;C-X&gt;&lt;C-O&gt;</tt> opens the omnicompletion manually. A preview window should also appear on the top which shows the signature of functions and the like. Choose <tt>vector</tt> and continue:
+
A box opens with suggestions. You can use <code>&lt;C-N&gt;</code> and <code>&lt;C-P&gt;</code> to navigate. <code>&lt;C-X&gt;&lt;C-O&gt;</code> opens the omnicompletion manually. A preview window should also appear on the top which shows the signature of functions and the like. Choose <code>vector</code> and continue:
 
<pre>
 
<pre>
 
std::vector<int> vi;
 
std::vector<int> vi;
 
vi.
 
vi.
 
</pre>
 
</pre>
Another box opens with suggestions. Choose <tt>push_back(</tt> and continue.
+
Another box opens with suggestions. Choose <code>push_back(</code> 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.
 
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.
Line 92: Line 92:
   
 
==Comments==
 
==Comments==
If you want completion to work for local variables in your code, you have to add the '<tt>+l</tt>' (letter "l" from local) option to the <tt>c++-kinds</tt> argument, as in:
+
If you want completion to work for local variables in your code, you have to add the '<code>+l</code>' (letter "l" from local) option to the <code>c++-kinds</code> argument, as in:
 
<pre>
 
<pre>
 
map <C-F12> :!ctags -R --c++-kinds=+pl --fields=+iaS --extra=+q .<CR>
 
map <C-F12> :!ctags -R --c++-kinds=+pl --fields=+iaS --extra=+q .<CR>
Line 98: Line 98:
   
 
----
 
----
Some recent edits have introduced <tt>--sort=1</tt> (the "1" should be "yes") and <tt>--sort=yes</tt>. I believe ctags defaults to <tt>sort=yes</tt> so these edits are not necessary. I'm wondering if the edits are useful. [[User:JohnBeckett|JohnBeckett]] 23:32, May 28, 2010 (UTC)
+
Some recent edits have introduced <code>--sort=1</code> (the "1" should be "yes") and <code>--sort=yes</code>. I believe ctags defaults to <code>sort=yes</code> so these edits are not necessary. I'm wondering if the edits are useful. [[User:JohnBeckett|JohnBeckett]] 23: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:
 
The autocompletion does *NOT* work for boost shared pointers. So for example, if there is a section of code that looks like:

Revision as of 06:38, July 13, 2012

Tip 1608 Printable Monobook Previous Next

created 2008 · complexity basic · author Knue · version 7.0


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.

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. JohnBeckett 23: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.


I suggest to use GCCSense at http://cx4a.org/software/gccsense, or clang_complete at script#3302 to have good C++ autocompletion that works with modern C++ code (e.g boost).

Around Wikia's network

Random Wiki