Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one navigate Ruby methods in VIM?

Tags:

vim

ruby

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?

like image 226
btelles Avatar asked Aug 25 '09 09:08

btelles


4 Answers

:help ]m

I think it requires vim-ruby for ruby support.

like image 194
eremite Avatar answered Nov 13 '22 16:11

eremite


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

like image 42
chillitom Avatar answered Nov 13 '22 18:11

chillitom


Best solution for Vim: use ctags. Read Vim documentation about how to navigate in TAGS files, also install plugin like CtrlP which allows you to visually browse tags.

Warning: Exuberant ctags does not work well with Ruby, the parser is not in good condition and it has not been changed 4 years now.

  • ctags doesn't deal with: module A::B
  • ctags doesn't tag (at least some of) the operator methods like ==
  • ctags doesn't support qualified tags, -type=+
  • ctags doesn't output tags for constants or attributes.

Unfortunately all the others (I found 2) Ruby ctags generators are either outdated (no Ruby 1.9+ support) or very slow.

There is one solution tho. Ripper-ctags: https://github.com/tmm1/ripper-tags It is fast and it works as expected. It is based on Ruby 1.9+ feature called "Ripper" which allows us to build on top of (fast) Ruby original parser. It is the most accurate ctags generator today.

Ripper CLI options are almost identical to ctags, so if you already know ctags, you will find ripper-tags easy to learn. It's as easy as:

ripper-tags -R .

This creates TAGS file which vim automatically reads by default (must be the directory where you open your vim instance in, or manually change path setting in vim if you start it in a different directory - more in the Vim manual).

If you like this, you can go step further and install my project which automatically creates TAGS for all the gems you install: https://github.com/lzap/gem-ripper-tags

Usage is very simple (note again, only Ruby 1.9+):

gem install gem-ripper-tags

Then generate tags for all already installed gems:

gem ripper_tags

Anytime you install a gem now, tags will be automatically created.

gem instal some_gem ...

I go one additional step further - I have a git template which is regenerating my project TAGS after every git pull or merge automatically (using ripper-tags):

https://github.com/lzap/bin-public/blob/master/git-hooks-reinstall

Note you will need directory files/git_template as well from the very same git repository.

I hope this is good starting point for navigating in Ruby codebases :-)

like image 14
lzap Avatar answered Nov 13 '22 17:11

lzap


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
like image 5
DrAl Avatar answered Nov 13 '22 16:11

DrAl