CTags generates index file of all your classes, methods and all other identifiers. You can use that index in your editor to jump straight to the methods you’re interested in. In this article, I’ll show you how to use them with Vim and Rails.
You need to install Exuberant CTags, on OSX just run
$ brew install ctags
or on Ubuntu/Debian
$ sudo apt-get install exuberant-ctags
In your rails project you can generate CTags for you project with
$ ctags -R --languages=ruby --exclude=.git --exclude=log .
But what about generating CTags for our bundled libraries too? Easy task, let’s add bundle paths
$ ctags -R --languages=ruby --exclude=.git --exclude=log . $(bundle list --paths)
You can jump into the method with
or using regular expressions like that
CTRL+]it will take you into the method.
CTRL-Twill take you back from that method.
CTRL-Owill take you In and Out from the method.
:ts [expr] # Lists tags matching expression :[count]tn # Jumps to the next matching tag :[count]tp # Jumps to the previous matching tag :[count]tf # Jumps to the first matching tag :[count]tl # Jumps to the last matching tag
If you’re using CtrlP, you can use
CtrlPTag to browser your tags. You can bind that command to a key and add this line to your .vimrc
nnoremap <leader>. :CtrlPTag<cr>
Since we want to DRY this workflow a little, we’ll install Ruby gem to auto-generate those tags for us.
We’ll be using Guard-CTags-Bundler
Install the gem:
$ gem install guard-ctags-bundler
Add it to your Gemfile (inside development group):
group :development do gem 'guard-ctags-bundler' end
Now add it to your Guardfile with
$ guard init ctags-bundler
Now you can run
guard and it will start watching your files will generate tags and gems.tags files. Since Vim is not looking for gems.tags by default, you’ll need to edit your .vimrc and add
set tags+=gems.tags line, then restart Vim.
Since you don’t want to commit those files into the GIT index, add them to .gitignore or I like to add them to my global ~/.gitignore.
$ cat <<EOT >> ~/.gitignore tags gems.tags EOT
Path depends on your configuration
git config --global core.excludesfile "~/.gitignore"
Vim has very nice build-in help that can be accessed by
I like to inspect Gems a lot. Just set your
$EDITOR variable and then run command
bundle open rails to open it in your favourite editor. I recommend you to try Tim Pope’s automatic RubyGems CTags invoker gem-ctags.
Just install the gem
$ gem install gem-ctags
and generate CTags for already installed Gems (needs to be run only first time).
$ gem ctags