Vim C++ auto complete - c++

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 &

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.

Detailed instructions Auto complete (archive.org) 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.

My favorite is clang_complete here. It's very easy to install and the default configuration in the ReadMe document works good. You don't need to generate the tags,
It automatically show the complete options when available. It also can highlight the
syntax errors.

Related

Generate CTAGS for libstdc++ (from current GCC)

I know YoucompleteMe on base of LLVM, but I want to use OmniCppComplete. This works nice, if I use the modified headers for C++. This modified headers are outdated and doesn't contain anything from C++11.
If noticed that I can modifiy my headers myself e.g.:
$ find . -name '*.h' | xargs sed -i 's/__STL_BEGIN_NAMESPACE/namespace std {/'
$ find . -name '*.h' | xargs sed -i 's/__STL_END_NAMESPACE/}/'
Or use this setting:
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
Both doesn't work and of course most headers doesn't have and any file extensions. I've already tried to workaround this by using a list of files. How can I create working CTAGS on base of my current GCC (e.g. /usr/include/c++/...)? What is the common way?
Thank you
This is what I got, if I try to complete something from LIBSTD++:
std::fs
Omni completion (^O^N^P) Back at original
Ctrl+x, Ctrl+o
Omni completion (^O^N^P) Pattern not found
Finally I've written despairingly an email to the author of the modified headers for the LIBSTDC++ (GCC). He answered me promptly (thanks!):
Requirements:
Install vim and vim-omnicppcomplete and ctags (dependency of vim-omnicppcomplete).
Solution:
$ cp -R /usr/include/c++/$GCC_VERSION ~/.vim/cpp_src
# it is not necessary to rename headers without an extension
# replace the "namespace std _GLIBCXX_VISIBILITY(default)" with "namespace std"
$ find . -type f | xargs sed -i 's/namespace std _GLIBCXX_VISIBILITY(default)/namespace std/'
$ ctags -f cpp_tags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -I _GLIBCXX_NOEXCEPT cpp_src
Edit your ~/.vimrc:
" configure tags - add additional tags here or comment out not-used ones
set tags+=~/.vim/cpp_tags
" build tags of your own project with Ctrl-F12
map <C-F12> :!ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q -I _GLIBCXX_NOEXCEPT .<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 ::
" also necessary for fixing LIBSTDC++ releated stuff
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
Autocompletion for LIBSTDC++ should now work within vim!
Manuall autocomplete with Ctrl+x -> Ctrl+o.
If you still have trouble with autocomplete, you maybe find a solution in the documentation (see FAQ 7).
Bye
After try hard to search how to create c++-specific tags file using ctags, finally I made desirable progress. Need to say, ctags indeed is a little hard to use, powerful though.
First to emphasize, DONOT ignore -I option (see here for more option info) provided by ctags. It's pretty important for you to see more symbols in the tags file. Simply speaking, ctags cannot process some irregular syntactic statements, for example, prefixed or suffixed with C/C++ macros. If many functions are prefixed or suffixed with some kind of macros, ctags may be very likely to drop it. In the end, you cannot see them when coding.
Here's a way I use to find such macros as more as possible from C++ standard header path, /usr/include/c++/4.9.3 for example, and add them to -I option.
find /usr/include/c++/ -type f -exec grep '_GLIBCXX_' {} \; | grep -v -E '^[#if|#end]' | sort -u > glibcxx.log
After the command, you'll find several macros used in C++ 4.9.3 source files, then choose some of them to be ignored by ctags.
My final command to generate c++ tags file is this:
ctags -R --languages=c++ --langmap=c++:+.tcc. --c++-kinds=+p --fields=+iaS --extra=+qf -I "_GLIBCXX_NOEXCEPT _GLIBCXX_USE_NOEXCEPT _GLIBCXX_NOTHROW _GLIBCXX_USE_CONSTEXPR _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _GLIBCXX_END_NAMESPACE_CONTAINER _GLIBCXX_CONSTEXPR _GLIBCXX_NAMESPACE_LDBL _GLIBCXX_BEGIN_NAMESPACE_VERSION _GLIBCXX_END_NAMESPACE_VERSION _GLIBCXX_VISIBILITY+" -f gcc-4.9.3.tags /usr/include/c++/4.9.3/
Caveat
DONOT forget to write . sign following word tcc, it means match those files without explicit extension name such as vector,set
DONOT forget to write + sign following word VISIBILITY, it means match std _GLIBCXX_VISIBILITY(**)
DONOT forget to write + sign followed by .tcc, it means append the denoted extension to current map of the langugae, otherwise replace the map
I actually use OmniCppComplete too, and have been happily using a C++ only tags file (amongst others. I break my tags files up into smaller pieces, like C++, boost, X11, etc.) for quite a while. Here's the solution I use for generating that tags file on Ubuntu 14.04:
ctags -f cpp_tags --c-kinds=cdefgmstuv --c++-kinds=cdefgmstuv --fields=+iaSmKz --extra=+q --langmap=c++:+.tcc. --languages=c,c++ -I "_GLIBCXX_BEGIN_NAMESPACE_VERSION _GLIBCXX_END_NAMESPACE_VERSION _GLIBCXX_VISIBILITY+" -n $INC_DIR/* /usr/include/$CPP_TARGET/c++/$CPP_VERSION/bits/* /usr/include/$CPP_TARGET/c++/$CPP_VERSION/ext/* $INC_DIR/bits/* $INC_DIR/ext $SYSTEM/* $SYSTEM2/*
Where:
CPP_VERSION=4.8
INC_DIR=/usr/include/c++/$CPP_VERSION
CPP_TARGET=x86_64-linux-gnu
SYSTEM=/usr/lib/gcc/x86_64-linux-gnu/4.8/include
SYSTEM2=/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
Please note that the real trick to getting most of the tags generated is the -I option! It may need to be tweaked. Of course, the --c-kinds/c++-kinds and fields options can be adjusted as well.
The final piece is adding:
tags+=cpp_tags
to your .vimrc file to allow the tags to be seen.
All of this requires NO MODIFICATIONS to the headers. If you use a different C++ library, chances are you'll need to fiddle with the -I option to get the tags to show up properly (or even at all).
Now that I think about it, I should probably post this information in the Vim Wiki also.
Your second part about let OmniCpp_DefaultNamespaces can stand some improvement. Try let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD", "_GLIBCXX_STD_A", "_GLIBCXX_STD_C"]. That's what I use and should allow for more items to be found.
Finally, don't forget about using Vim's CTRL-p to complete the std::fs. It's quite powerful... and no tags file required!

auto fold for C++ sources and headers

I have heard that Vim has a built-in support of folding for the files written in various programming languages. Particularly I'm interested in cpp, h, hpp files. I would like to achieve such a behavior of Vim when all the function definitions are folded by default in every newly opened source file. I don't want to create every fold manually with zF.
I added the following lines to the end of ~/.vimrc file (which was inherited from spf13-vim distribution):
set foldenable
autocmd FileType c,cpp,h,hpp setlocal foldmethod=syntax
Unfortunately that did not resulted to the desired behaviour. All the files look just the same, and my attempt to hide the function with zc combo leads to error:
E490: No fold found
I would be glad if someone could explain how to enable folding by default in Vim 7.4.
UPD: The code above works good, but there is still an issue with the recently opened files. Say we work on the project called 'sc-client-server' and we've just appended those two lines to ~/.vimrc and now ready to get back to the last edited file:
$ vim ~/.vimrc //Modify config
$ reboot
$ vim ~/progs/sc-client-server/src/st.cpp //No fold found. BUT:
$ cp ~/progs/sc-client-server/src/st.cpp /tmp
$ vim /tmp/st.cpp //Auto fold works fine. OR:
$ cd ~/progs/sc-client-server/src/ && mv src source
$ vim ~/progs/sc-client-server/source/st.cpp //Auto fold works fine.
Is there any kind of cache or memory inside of Vim that prevents the desired file representation?
Answer
Remove all (or specific for you) files from ~/.vimviews folder.
To the off-topic voters https://meta.stackexchange.com/questions/25925/vim-questions-do-they-belong-to-stack-overflow-or-super-user/25930#25930?newreg=00a4918bba924cfa8b1d6a3e82061672

Using ctags and cscope

I did a ctags -R on my project which is in c++, in the directory /project/ntopng. Now, when I start cscope using cscope -R and search for main.cpp, it opens up. But, when I hit ctrl-] on #include "ntop-includes.h" in main.cpp, the error message is tag not found. The header file is inside a sub-directory in /project/ntopng/include. But, ctags -R is recursive so why is it that I am getting an error? I am using Ubuntu 12.04 with the latest version of ctags and cscope. Thank You.
I have given the below answer for Ubuntu 12.04
1. Open any file with vim
2. type :echo &tags ,It will show what path vim is using for tags file.
If it is not the expected tag file path type:
:set tags=path_to_your_tag_file (ex /project/ntopng/tags)
Remember it is valid for the current session only, Now if permanent changes required there are two options.
For All users (requires root privileges) --
1. cd /etc/vim
2. vim vimrc
3. Go to end and add set tags+=tags;path_to_your_tag_file
For the individual user:
1. cd ~
2. vim .vimrc (This file may not exists in that case newly created)
3. set tags+=tags;path_to_your_tag_file
Let me know if it worked for you.

Vim, C++, YCM, and Syntastic include path problems

I feel that I have an awesome setup for C++ programming using Vim but I can't find a way to tell Vim, YCM, and Syntastic where to search for headers. It would be really annoying to have to manually set the include path variables for Vim, YCM, and Syntastic every time I want to work on a project when this information exists in the Makefile. Is there any automated solutions for setting a global include path?
Edit: It won't even find the headers if I set the path like this ":set path = ".,/usr/include,include,../include,/home/steven/ovgl/include,,""
Your headers should appear in your tag files (see :h tags if you don't know about it).
Then YouCompleteMe is able to read the information about your headers from the tag file, as explained in the plugin faq:
YCM does not read identifiers from my tags files
First, put let g:ycm_collect_identifiers_from_tags_files = 1 in your vimrc.
Make sure you are using Exuberant Ctags to produce your tags files since
the only supported tag format is the Exuberant Ctags format. The format
from "plain" ctags is NOT supported. The output of ctags --version should
list "Exuberant Ctags".
Ctags needs to be called with the --fields=+l (that's a lowercase L, not a
one) option because YCM needs the language:<lang> field in the tags
output.
NOTE: Mac OS X comes with "plain" ctags installed by default. brew install
ctags will get you the Exuberant Ctags version.
Also make sure that your Vim tags option is set correctly. See :h 'tags'
for details. If you want to see which tag files YCM will read for a given
buffer, run :echo tagfiles() with the relevant buffer active. Note that
that function will only list tag files that already exist.
You shold look for YCM-Generator. It is a script that generates ycm_extra_conf.py by running make and looking for all flags used. You run it once for project, and rerun only when make file changed.
I had faced a similar issue. I needed this for use with development using llvm.
I solved it by following the below steps:
Ctags -R --fields=+l * in your project/code base.
In your user .vimrc file, add let g:ycm_collect_identifiers_from_tags_files = 1
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py ~/
Add another line in .vimrc let g:ycm_global_ycm_extra_conf = '/home/<user>/ycm_extra_conf.py'
reset terminal or hit bash
Note: You should start vim in the directory with the tags present in it. Or you may need to explicitly specify the directory where the tags are present.

Using two asterisks to add a file in git

I want to add a file which has a unique file name but a long preceding path (e.g. a/b/c/d/filename.java). Normally I would add this to my repository by doing
git add *filename.java.
However I have also done this before:
git add a/b/c/d/filename*
So I tried to combine the two:
git add *filename*
but this does something weird. It adds every untracked file. I can see possible reasons for failure but they all should occur in one of the previous two commands so I don't know why this is happening.
My question isn't so much about how to add a file to a git repository with just its file name (although that would be useful).
My question is what is my misunderstanding of the * operation which makes me think the above should work.
Info:
I am using Git Bash for Windows, which is based on minGW.
You're looking at globs
(not regular expressions, which are a different pattern-matching language), and they're expanded by your shell, not by git.
If you want to see how they're going to match, just pass the same glob to another command, eg.
$ ls -d *filename.java
vs
$ ls -d *filename*
(I've just added the -d so ls doesn't show the contents of any directories that match)
Since you're using git bash, and it's possible that glob expansion behaves differently from a regular shell, try
$ git add --dry-run --verbose -- *filename*
for example: this should show you how it really expands the glob and what effect that has.
Note the -- ... if you're using globs that might match a filename with a leading -, it's important to make sure git knows it's a filename and not an option.
Unfortunately, this will only show you the files which both match the glob, and have some difference between the index and working copy.
Answer from author:
The dry run helped a lot, here is what I found:
I was forgetting about the bin folder which I haven't added, so when I performed the dry run I realised it was finding two matches: filename.java and filename.class. When I changed the glob to *filename.j* it worked.
My next step was to remove the .class and try the command again: it worked! It is still unexplained why git bash added everything when it found two matches... since the dry run behaves differently from the actual run I think there must be a bug, but I think that discussion is to be held elsewhere (unless somebody thinks it isn't a bug).
You could try with git add ./**/*.java
Note: I tested with zsh, it should also work for bash as well.