tags:

views:

15841

answers:

15

I have just installed Ctags (to help with C++ development) with my Vim (or rather gVim), and would like to find out your favorite commands, macros, shortcuts, tips that go along with it...

Share your best arsenal. What other Vim add-ons you would recommend for C++ on Vim development?

EDIT What other add-on you would use in conjunction with Ctags?

EDIT2 What version of gVim you use with tags? Does it make a difference?

EDIT3 How do you enhance your programming experience for both big and small projects?

+7  A: 

I use ALT-left and ALT-right to pop/push from/to the tag stack.

" Alt-right/left to navigate forward/backward in the tags stack
map <M-Left> <C-T>
map <M-Right> <C-]>

If you use hjkl for movement you can map <M-h> and <M-l> instead.

strager
isn't the same as ctrl -] and ctrl-t -- I guess, your suggestion is a bit cleaner.
@Sasha, Pardon? Don't know what you're saying. =X To note, this also works in :help.
strager
I think this is more intuitive and cleaner than the default.
Ayman
+7  A: 

You can add directories to your ctags lookup. For example, I have a ctags index built for Qt4, and have this in my .vimrc:

set tags+=/usr/local/share/ctags/qt4
strager
+4  A: 

The command I am using most is C-] which jumps to the definition of the function under the cursor. You can use it more often to follow more calls. After that, C-o will bring you back one level, C-i goes deeper again.

Raim
C-t goes back one level in the C-] stack, while C-o goes back to a previous cursor position after a movement. So if you C-] to a function then page down, C-t will go back immediately while C-o will go back to the start of the function, *then* back to the call.
Greg Hewgill
+35  A: 

C-] - go to definition
C-T - Jump back from the definition.
C-W C-] - Open the definition in a horizontal split

Add these lines in vimrc
map <C-\> :tab split<CR>:exec("tag ".expand("<cword>"))<CR>
map <A-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR>

C-\ - Open the definition in a new tab
A-] - Open the definition in a vertical split

After the tags are generated. You can use the following keys to tag into and tag out of functions:

Ctrl-Left_MouseClick - Go to definition
Ctrl-Right_MouseClick - Jump back from definition

jinxed_coder
Thanks for the definition-in-new-window trick! =]
strager
C - ] => (control key) and ] key C - T => (control key) and T key Just for newbies to ctags. Thanks for new tab trick
kumar
Similarly, after splitting the window with `Ctrl-w C-]` you can do `C-w T` (aka Shift-t) to change that split into a new tab.
dash-tom-bang
+1  A: 

I've encapsulated tags manipulation in an experimental plugin of mine.

Regarding C++ development in vim, I've already answered there: I use my own suite, and a few other plugins.

Luc Hermitte
+12  A: 

Another useful plugin for C development is cscope Just as Ctags lets you jump to definitions, Cscope jumps to the calling functions.

If you have cscope in your ~/bin/ directory, add the following to your .vimrc and use g^] to go to the calling function (see :help cscope).

if has("cscope")
    set csprg=~/bin/cscope
    set csto=0
    set cst
    set nocsverb
    " add any database in current directory
    if filereadable("cscope.out")
        cs add cscope.out
        " else add database pointed to by environment
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif
endif

Almost forgot... Just as ctags - you have to generate (and periodically update) the database. I use the following script

select_files > cscope.files
ctags -L cscope.files
ctags -e -L cscope.files
cscope -ub -i cscope.files

Where 'select_files' is another script that extracts the list of C and header files from the Makefile. This way I index only the files actually used by the project.

nimrodm
What is the advantage of cscope over ctags? Do they compliment eachother?
Whaledawg
They complement each other. Ctags finds the definition of a symbol or a function. Cscope can find all the places calling a function. using both of them makes navigation in large source trees very easy.
nimrodm
However, AFAIK cscope has a limited comprehension of C++.
Luc Hermitte
It is limited, and not very good. But for what it does (jumping to USAGES), there isn't any better alternative. Its main problem is that when you want to jump to function calls (f), you often need to jump to anywhere that symbol is used (s) instead and filter the results mentally...
Greg Rogers
...because it hates namespaces. Global is also workable, but IMO slightly worse.
Greg Rogers
Can you show an example of select_files scripts ?
vehomzzz
A: 

I've found the taglist plug-in a must-have. It lists all tags that it knows about (files that you have opened) in a seperate window and makes it very easy to navigate larger files.

I use it mostly for Python development, but it can only be better for C/C++.

Walter
A: 

I've been adobting my vim plugins for two years to support big enough c++ project. You can take a look at them.

They use ctags and cscsope.

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

Mykola Golubyev
+5  A: 

All of the above and...

code_complete : function parameter complete, code snippets, and much more.

taglist.vim : Source code browser (supports C/C++, java, perl, python, tcl, sql, php, etc)

BR Johan

Johan
I think it would be good to add that taglist.vim is the most downloaded vim plugin.
Amit Kumar
+26  A: 

One line that always goes in my .vimrc:

set tags=tags;/

This will look in the current directory for "tags", and work up the tree towards root until one is found. IOW, you can be anywhere in your source tree instead of just the root of it.

uzi
Going up to root is excessive. I suggest only going up to home instead: tags+=tags;$HOME
gotgenes
The source trees I work on aren't always in my home directory... and being that $HOME tends to be two directories away from root... well, I can afford two more directory lookups. :)
uzi
I agree w/ uzi. If my cwd is /home/me/programming/foo/bar/baz/bang/bam that's only 8 directory lookups. I suppose it depends on your tolerance for speed, but the time to lookup 1 directory isn't noticeably different from 8 on my machine.
Nate Murray
just discovered a bug in this: if there is a directory called /home/jack/proj/trunk and another called /home/jack/proj/trunk_too, searching for tags in trunk_too directory also returns tags from trunk directory. Same for any trunk* directory. The correct answer is: set tags=./tags;/ (not sure exactly why though).
Amit Kumar
sweeeeet. this is a much better solution to what i was looking for.
Pestilence
I put my tags into a subdirectory, but use a similar trick (that yields the same fix mentioned by @Amit), `set tags=_tags/tags;c:/work`
dash-tom-bang
+2  A: 

I put the following in my .gvimrc file, which searches up the tree from any point for a tags file when gvim starts:

function SetTags()
    let curdir = getcwd()

    while !filereadable("tags") && getcwd() != "/"
        cd ..
    endwhile

    if filereadable("tags")
        execute "set tags=" . getcwd() . "/tags"
    endif

    execute "cd " . curdir
endfunction

call SetTags()

I then periodically regenerate a tags file at the top of my source tree with a script that looks like:

#!/bin/bash

find . -regex ".*\.\(c\|h\|hpp\|cc\|cpp\)" -print | ctags --totals --recurse --extra="+qf" --fields="+i" -L -
alanwj
You just re-invented upward search. See :h file-searching. See also http://stackoverflow.com/questions/563616/vimctags-tips-and-tricks/741486#741486
gotgenes
A: 

Initially, I have specified tag path names completely and struggled a lot in finding why ctags is not working in gvim.

set tags=./tags,C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\tags

Actual problem is that ctags on windows requires the tag path names to be specified in old dos (8.3 notation) style pathnames.

set tags=./tags,C:\PROGRA~1\APACHE~1\Apache2.2\htdocs\tags,C:/PROGRA~1/MIEEF7~1/tags

Although specifying tag path names in dos notation works,any thoughts on some other best workaround for this?

Naga Kiran
A: 

@alanwj,

I do not know why, but with your SetTags function in .vimrc, cscope is going crazy.

I am running 'cscope -d' in foo/bar directory. Then the file opened for editing has a pwd 'foo' instead of 'foo/bar'.

Please fix this up, if you can..

thanks, Mallik

A: 

@Naga Kiran: You likely would have needed to quote those space'y pathnames.

Anonymouse
A: 

Several definitions of the same name

<C-w>g<C-]> open the definition in a split, but also do :tjump which either goes to the definition or, if there are several definitions, presents you with a list of definitions to choose from.

Heikki Naski