tags:

views:

181

answers:

4

How do I enable auto completion in Vim?

I tried to do this one, but I'm not proficient with the vimrc file, etc., so it didn't work out. Can you give me step by step instructions on how to do this?


Edit

I tried installing OmniCppComplete. Followed the instructions, but when I try to use it I get the following error:

Error detected while processing function omni#cpp#complete#Main..24_InitComplete:

line 24:

E10: \ should be followed by /, ? or &

+2  A: 

Detailed instructions Auto complete Type in first few characters and press Ctrl->P(for backward search) or Ctrl->N(for forward search), list down all options available or completes it.

I use vim7.2(auto complete was introduced in vim7) and these controls work just fine.

DumbCoder
Does this work for included files?
Amir Rachum
Yes in vim7, not sure if available in older versions.
DumbCoder
I think this only works by scanning other words used in the current file or a dictionary file you specify. In Vim, type ":help compl-generic" for details on how this works, and its limitations.
NeilDurant
It works for header files also. I don't have a dictionary file and Ctrl N and P resolved for functions declared in the header files.
DumbCoder
+1  A: 

Vim by default will do completion based on words in the file using Ctrl-N or Ctrl-P, which is handy for recently referenced local variables etc, and works for code in any language or even ordinary text (handy for completing difficult to spell names). However it doesn't do this semantically or with reference to what actual types you're allowed in the particular context you're writing. For this you will need to install ctags, and then in /usr/include type:

ctags -f ~/.vim/stdtags -R --c++-kinds=+p --fields=+iaS --extra=+q .

And then add this to your .vimrc:

set nocp
filetype plugin on
map <C-L> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR>

set tags=~/.vim/stdtags,tags,.tags,../tags

autocmd InsertLeave * if pumvisible() == 0|pclose|endif

That will also make Ctrl-L reload tags, and thus pick up new autocomplete tags, from the current directory.

NeilDurant
What does ctags do? Can you explain what the commands you've given do? Will this only work for local stuff? I need this to work in a large project.
Amir Rachum
Will this also autocomplete from included header files?
martiert
Ctags indexes the code you write and stores semantic information about it, which can then later be queried for things like finding word completions, locating usages of functions/variables etc."set nocp" takes vim out of "compatible" mode, which is required for the following commands. "filetype plugin on" enables vim to do things based on file type (i.e. C++). The "map <C-L>..." line sets a key mapping for Ctrl-L to call ctags to update its index. The "set tags=..." line defines where tag information is stored, which are standard places. The "autocmd" line dismisses the completion window.
NeilDurant
In answer to martiert, it will autocomplete with correct semantics for any files ctags indexes, which will be all C++ files in the current directory or its subdirectories, which will generally include included header files. So for a large project, if you go to the root of the source tree and hit Ctrl-L, it should index everything in the project.
NeilDurant
A: 

Also check out omni-complete (IntelliSense) script for C++: http://www.vim.org/scripts/script.php?script_id=1520

Nemanja Trifunovic
A: 

Vim can use the Eclipse auto completion through the Eclim tool (http://eclim.org/vim/c/complete.html). In such case the Eclipse IDE is used as a headless server.

walter.dicarlo