How to navigate Ruby methods in VIM?

I am learning VIM for Rails development and would like to easily navigate through the methods in the file. So far, I see several options:

  • Find 'def' using

    /def<space> 
  • Create a macro matching the key with q and write
  • use vim marks? (not even sure what they are doing, they just sound promising.

Anyone have any better ideas?

+41
ruby vim
Aug 25 '09 at 9:10
source share
7 answers

: help] m

I think vim-ruby is required to support ruby .

+37
Sep 02 '09 at 16:50
source share

you will need the ctags function

see exuberant ctags , it works in many languages, including Ruby, and v is easy to use.

from VIM :help ctags

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

In addition, you can look at this page that describes how to use VIM as a full-featured Ruby IDE: Using Vim as a full Ruby On Rails IDE

+34
Aug 25 '09 at 9:13
source share

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

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

  • ctags is not related to: module A :: B
  • ctags does not tag (at least some of) operator methods, such as ==
  • ctags does not support qualified tags, -type = +
  • ctags does not display tags for constants or attributes.

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

There is one solution tho. Ripper-ctags: https://github.com/tmm1/ripper-tags It works fast and works properly. It is based on a Ruby 1.9+ function called "Ripper" that allows us to build on top of (fast) original Ruby parsing. This is the most accurate ctags generator today .

The CLIP Ripper options are almost identical to ctags, so if you already know ctags, you will find it easy to read ripper tags. This is easy:

 ripper-tags -R . 

This creates a TAGS file that vim automatically reads by default (there must be a directory in which you open your vim instance, or manually change the path settings in vim if you run it in another directory - more in the Vim manual).

If you like it, you can go ahead and install my project, which automatically creates TAGS for all the stones you set : https://github.com/lzap/gem-ripper-tags

Using is very simple (note only Ruby 1.9+):

 gem install gem-ripper-tags 

Then create tags for all stones already installed:

 gem ripper_tags 

Any time you set a gem, tags will be automatically created.

 gem instal some_gem ... 

Next, I will add another step: I have a git template that regenerates my TAGS project after each git automatically extends or merges (using ripper tags):

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

Note that you will need the / git _template directory files from the git repository itself.

Hope this is a good starting point for navigating Ruby's codebases :-)

+12
Jul 22 '13 at 11:10
source share

A few ideas:

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

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

Then you can use [[ and ]] to go back and forth functions, as 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 move around the bend. You can also install this plugin so you can easily remove creases with daz .

To search for specific functions (and not just navigate around them) you will want to use ctags (as mentioned in chillitom). taglist plugin greatly facilitates the transition to a specific function, but (as chillitom said) Ctrl - ] and Ctrl - T are useful for the following keywords under the cursor.

For more information see

 :help [[ :help ft-ruby-syntax :help z[ :help after-directory 
+5
Aug 25 '09 at 9:41
source share

I recently discovered that Ruby.vim (one of the answers above) comes with pretty useful key bindings:

 nnoremap <silent> <buffer> [m :<CU>call <SID>searchsyn('\<def\>','rubyDefine','b','n')<CR> nnoremap <silent> <buffer> ]m :<CU>call <SID>searchsyn('\<def\>','rubyDefine','','n')<CR> nnoremap <silent> <buffer> [M :<CU>call <SID>searchsyn('\<end\>','rubyDefine','b','n')<CR> nnoremap <silent> <buffer> ]M :<CU>call <SID>searchsyn('\<end\>','rubyDefine','','n')<CR> xnoremap <silent> <buffer> [m :<CU>call <SID>searchsyn('\<def\>','rubyDefine','b','v')<CR> xnoremap <silent> <buffer> ]m :<CU>call <SID>searchsyn('\<def\>','rubyDefine','','v')<CR> xnoremap <silent> <buffer> [M :<CU>call <SID>searchsyn('\<end\>','rubyDefine','b','v')<CR> xnoremap <silent> <buffer> ]M :<CU>call <SID>searchsyn('\<end\>','rubyDefine','','v')<CR> nnoremap <silent> <buffer> [[ :<CU>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','b','n')<CR> nnoremap <silent> <buffer> ]] :<CU>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','','n')<CR> nnoremap <silent> <buffer> [] :<CU>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','b','n')<CR> nnoremap <silent> <buffer> ][ :<CU>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','','n')<CR> xnoremap <silent> <buffer> [[ :<CU>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','b','v')<CR> xnoremap <silent> <buffer> ]] :<CU>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','','v')<CR> xnoremap <silent> <buffer> [] :<CU>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','b','v')<CR> xnoremap <silent> <buffer> ][ :<CU>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','','v')<CR> 
+3
Nov 25 2018-11-11T00:
source share

One trick is to simply search using the ' /f methodName '.

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

 :let ruby_fold 

See :help ft-ruby-syntax details.

+1
Aug 25 '09 at 9:22
source share

Usually I just type the name of the method for incremental searches.

0
Aug 25 '09 at 9:14
source share



All Articles