tags:

views:

628

answers:

5

I'm learning VIM for Rails development and would like to easily navigate methods in a file. So far I see several options:

  • Find 'def' by using

    /def<space>
    
  • Create a macro that corresponds to a key using q and record
  • use VIM marks? (not even sure what they do, they just sound promising

Anyone have any better ideas?

+7  A: 

you'll want a feature called ctags

see exuberant ctags, it works for many languages included Ruby and is v simple to use.

from VIM :help ctags

ctags will create an index of all identifiers in a source tree. You can then use the tag commands to navigate around your source tree. see :help tag-commands. The easiest is to place the cursor over a keyword and press CTRL-]. To get back to where you came from press CTRL-T

Beyond this you might want to look at this page which documents how to use VIM as a more full featured Ruby IDE: Using Vim as a Complete Ruby On Rails IDE

chillitom
Perfect! Also, for anyone else who's interested, there's plugin called SimpleFold that does arguably more useful folding than using the regular ruby folding (\f or ruby_fold = 1). Here's the link:http://www.vim.org/scripts/script.php?script_id=1868
btelles
A: 

Usually I just type the name of the method on the incremental search.

Emmanuel Caradec
+1  A: 

One trick is to just search using '/f methodName'.

You should also look at turning on code folding by adding this line to your .vimrc:

:let ruby_fold

See :help ft-ruby-syntax for more details.

too much php
+2  A: 

A couple of ideas:

Firstly, make a mapping to use the C function searching keys in ~/.vim/after/ftplugin/ruby.vim:

:nmap [[ ?def <CR>
:nmap ]] /def <CR>

Then you can use [[ and ]] to go forward and back a function, just like in C/Perl/Java code etc.

Another way that might help:

In .vimrc, add the line:

:let ruby_fold = 1

Then use zj, zk, z[ and z] to navigate by folds. You could also install this plugin so that you can easily delete folds using daz.

For finding specific functions (rather than just navigating around them) you'll want to use ctags (as mentioned by chillitom). The taglist plugin makes it much easier to navigate to a specific function, but (as chillitom said) Ctrl-] and Ctrl-T are useful for following keywords under the cursor.

For more information, see:

:help [[
:help ft-ruby-syntax
:help z[
:help after-directory
Al
A: 

:help ]m

I think it requires vim-ruby for ruby support.

eremite