From charlesreid1

Vimtags: http://vim.sourceforge.net/scripts/script.php?script_id=273

Using Ctags with Vim: http://amix.dk/blog/post/19329


Basics

The Ctags web site is here: http://ctags.sourceforge.net/

Ctags documentation is here: http://ctags.sourceforge.net/ctags.html

Basic usage of Ctags is usually:

$ ctags
$ ctags -R

If you're using Ctags for C++, though, you'll want a more complex call:

ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q \
  --language-force=C++ /path/to/target/directory -f /path/to/output/tag_file

Using Ctags with Vim

There are a couple of different ways to use Ctags with Vim. I use two:

This StackOverflow thread has a lot of really useful tips and tricks for using Ctags with Vim: http://stackoverflow.com/questions/563616/vim-and-ctags-tips-and-tricks

Universal Ctags Vim Stuff

There are some things you can put in your .vimrc file that can be used by any plugin/etc using ctags.

Here is how I structure things:

1. Create tag files for any relevant C++ projects

2. Put these tag files in the directory ~/.vim/tags/

3. Specify the directories in which to look for tags

4. Other stuff that's handy to map to keyboard shortcuts

Creating Tag Files

You can create tag files using the "more complex call" specified above.

Tag Directory

The tag directory I use is in ~/.vim/tags. By default, the only place where Ctags looks for tag files is in the current directory, and for large software projects with complex directory structures, adding a tag file to every directory is stupid - not to mention it keeps you from being able to use all the tag information available for a project. Putting the tag information for an entire source code directory structure in a single file allows you to load all of that information in one shot. Putting it in a universal location allows you to load it whenever you want, no matter where you are.

Specifying Tag Directories

In your .vimrc file, you can specify tag files that you want to load every time you run vim:

set tags+=~/.vim/tags/cpp
set tags+=~/.vim/tags/uintah_src

Handy Ctag Shortcuts

You can map keyboard shortcuts for Ctag functions that are handy/useful to map.

This will map F6 to create a tagfile for the current directory and put it in the current directory:

map <F6> :!/path/to/ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ .<CR>


OmniCppCompletion

http://www.vim.org/scripts/script.php?script_id=1520

This Vim plugin allows for C and C++ omni-completion using a Ctags tag file.

Installation

You should follow the installation instructions.

Configuration/Setup

Once you have gotten it installed, add the following to your .vimrc:

" =============================================
" OmniCppComplete
" from http://www.vim.org/scripts/script.php?script_id=1520
" via http://vim.wikia.com/wiki/C%2B%2B_code_completion
set nocp
filetype plugin on

" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1

" show function parameters
let OmniCpp_ShowPrototypeInAbbr = 1 

" autocomplete after .
let OmniCpp_MayCompleteDot = 1

" autocomplete after ->
let OmniCpp_MayCompleteArrow = 1 

" autocomplete after ::
let OmniCpp_MayCompleteScope = 1 

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

Usage

To illustrate how Ctags can be used and how it looks...

1. Begin by editing a file with code that has been "tagged". In this case, this is a file in a large C++ project with multiple directories. I have tagged all of these source code files in a single tag file, which is in ~/.vim/tags/uintah_src.

OmniCppCompletion1.png

2. I can start typing Arches:: (Arches is the name of a class). This automatically opens a drop-down menu, from which I can choose ENUM types, public/private members, functions/constructors/destructors, etc.

OmniCppCompletion2.png

3. The menu can be navigated using (Arrow Up)/(Arrow Down) or by using (Control+N)/(Control+P) to move down/up (respectively). When I move the selector up/down, it opens a split pane at the top that has minimal information about what is selected (its type, level of protection, where it is defined, etc.)

Selecting an enum type:

OmniCppCompletion3.png

Selecting a constructor method:

OmniCppCompletion4.png

Selecting a private member:

OmniCppCompletion5.png

Also, as you begin to type a method, member, etc., the autocomplete menu will update:

OmniCppCompletion6.png

Additionally, you can use tag information from other classes. For example, if I want to call a method of a private member, I type the private member's name, then either a dot or an arrow, and the autocomplete pop-up will automatically appear.

OmniCppCompletion7.png

Vim TagList Plugin

http://vim.sourceforge.net/scripts/script.php?script_id=273

This Vim plugin is one of the most popular plugins.

Documentation is located here: http://vimdoc.sourceforge.net/htmldoc/tagsrch.html

Creating libstdc++ Tag File

http://www.vim.org/scripts/script.php?script_id=2358

You can download the above version of the libstdc++ libraries, and use this to create a Ctags tag file for the C++ std lib.

You can follow the instructions on the web page, and put the tag file in ~/.vim/tags and add it .vimrc, by adding it to the list of directories that Ctags looks for tag files (e.g. set tags+=~/.vim/tags/cpp).