tags:

views:

964

answers:

8

I am programming in C++ or Java. So I want to use Vim editor, because it is very flexible. I have heard that I can configure the Vim editor to be able to go

  1. from object to the definition
  2. from function to the definition
  3. from class name to the definition

Do we have any professional Vim-er that could tell me how exactly to configure Vim for that? Thanks in advance.

P.S. In case readers will consider this question is not connected with programming, I would say that this is improving speed of programming. So it is a help to the programmer. So please don't close this question.

EDIT: Also I would like to know how vim works with code completion and can vim hint the list of methods that are available for the certain object? If yes, then I would like to know how to configure these options too?

+8  A: 

What you're looking for is ctags and tags/TAGS files. Ctags (I recommend Exuberant Ctags) is a program which scans source files for identifiers and creates a file indexing them. You can then use ^] to jump to the definition for the tag under the cursor.

There may be some additional details needed to tell vim how to find the tags file; I'm not sure off-hand what they are. But that's the general idea - run ctags over your source code and then use ^].

Alternatively, you may wish to look at GNU Global. It's a bit more sophisticated. But ctags will accomplish what you've described.

Michael E
To telll vim where the tags file are, use the tags option. Type ":help 'tags'" in vim for details.
Laurence Gonsalves
+1  A: 

Code-completion in vim is available as vim-scripts: http://www.vim.org/scripts/index.php

Kartik Mistry
With version 7.0 and up you can auto-complete names and lines of code in insert mode using keystroke combinations like ctrl-P, ctrl-N, and ctrl-X-ctrl-L. The online help has all the details.
Steve K
A: 

Yes, the tool that provides this symbol index is called ctags, and many unix editors can use it. vim is nice on the command line, but I'd suggest spending a bit of time getting used to KATE. It's a very nice, modern GUI editor, with IDE-like file browsing, good syntax highlighting, the nicest dynamic wordwrapping I've ever used, etc. For java though, eclipse and netbeans are probably worth a look.

Lee B
+1  A: 

Vim is without a shadow of a doubt, the best editor in the world (Come get me emacs guys). Flame wars aside, what I have found incredibly useful for both C++ and Java programming is eclipse (best IDE in the world, yes now I am poking Netbeans) with the vrapper plugin. You get all the benefits of a fully integrated development environment and the power of vim keyboard shortcuts.

Vrapper doesn't give you full vim emulation but you can bounce around your code using vim shortcuts and you don't loose any of the goodness of eclipse.

John Oxley
For varying values of "best"...
Thorbjørn Ravn Andersen
It's actually for varying values of "world" but I see your point!
John Oxley
+1  A: 

ctags in general is the answer, as Michael E. said. ctags *.c *.h is a simple start for a project that has all the files in one directory. Then, as Michael said, you can use ^] to go to the definition and ^T to go back where you came from. These are the same keys you use to move around inside Vim's help.

In Java and C++ you may have more than one method with the same name. :tn will go to the next one, and :ts will list the alternatives.

For code completion in Vim, I usually use ^P and ^N. It's not context-sensitive like the equivalent in Eclipse or MSVC, but it usually does the job.

Other tools you might consider using are cscope and (of course) Emacs or Eclipse.

Kragen Javier Sitaker
+2  A: 

As others have said, you need to generate an index file for vi(m).

I just want to say that using vim for programming Java instead of a modern IDE, is like using vim plus a calculator instead of a spreadsheet for numbers. Both are ok for small tasks, but when scaling up you reach a point where the spreadsheet just leaves the text editor behind.

If you do java programming for a living, do yourself and your employer a favour and learn to use a power tool.

Thorbjørn Ravn Andersen
vim 7.2 supports floating point numbers, so there's no need for a calculator :)
intuited
Intriguing. I simply cannot guess what an editor needs floating point numbers for, so perhaps this is just a step more towards having an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp?
Thorbjørn Ravn Andersen
+1  A: 

Using ctags is definitely the place to start, but don't forget cscope. In both cases, you first have cscope and ctags scan your source files and create their databases before you start vim. You also need to occasionally refresh the databases as your code grows and changes.

With ctags, simply use the command "ctags" in your code directory. If you have a heirarchy of directories, start at the top and use "ctags -R" to recurse through all the directories below. Start vim in the same directory as the resulting tags file and vim should find and load it automatically. When in the editor with the cursor on a function or variable name, you can type '^]' and vim should jump to the definition.

Similarly, with cscope, use "cscope -b" or "cscope -b -R" to get more detailed tag information. Vim and cscope work together to give you much more information such as where an identifier is used and from where a function is called. There is a set of macros that can be used to access the information. You can find the macros and their meanings in the vim cscope help information.

Shannon Nelson
A: 

Last time I checked, a long while ago, ctags was okay, but it missed some identifiers, and cscope had a more exhaustive search option, but it still missed some identifiers.

grep is what I prefer. It's exhaustive but still pretty fast if the source code base is a reasonable size or the PC is fast. You can invoke it from vim with :grep. Here's a simplified version of my own _vimrc to show how I have it configured. I put comments to explain what keys do what.

" for Windows, replace with the location of `grep.exe'
" not needed for Unix/Linux
:set grepprg=c:\prog\cygwin\bin\grep.exe\ -n\ $*\ /dev/null


:function! RecursiveSearchIdentifierUnderCursor()
:   sp
:   let grep_cmd = "silent grep -r \"\\<" . expand("<cword>") . "\\>\" *"
:   exe grep_cmd
:endfunction


" CTRL-backslash to search for the identifier (or "word") under the cursor
"
" an identifier is in the form "xxx_123_yyy" but something in the form
" "xxx_123_yyy.aaa_999" would be two identifiers separated by a period
"
"
" this basically executes:
"
"       :grep -r "\<identifier\>" *
"
" for whatever `identifier' the cursor happens to be over
"
"
" to close the newly opened window, use:
"
"       :q
"
:map <C-\> :call RecursiveSearchIdentifierUnderCursor()<CR>


" cursor up/down to move to the previous/next search result
:map <Up> :cp<CR>zz
:map <Down> :cn<CR>zz


" jump up/down between horizontally split windows
:map <PageUp> <C-W>k
:map <PageDown> <C-W>j


" move to the previous/next enclosing brace
:map <Left> [{
:map <Right> ]}


" move to the previous/next enclosing parenthesis
:map <Home> [(
:map <End> ])


" move to the previous/next enclosing # preprocessor directive
:map <Insert> [#
:map <Del> ]#

Also, don't forget * (SHIFT-8) and # (SHIFT-3) to search forwards and backwards for the current identifier under the cursor within the current file. I've found these invaluable when jumping around through code, especially when searching for short variable names like 'i' and 'j' that would otherwise match parts of other variable names in a blind search in a conventional text editor.

You can also invoke :make from vim if you like to use make. It will stop on the first compilation error, then you can edit and start the build again from inside vim.

Once you get comfortable defining your own functions and key mappings, there are lots of ways to make repetitive programming tasks easier with vim.

Matthew