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>
Anyone have any better ideas?
:help ]m
I think it requires vim-ruby for ruby support.
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
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.
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 :-)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With