kdobmw
Last Updated: February 25, 2016
·
1.352K
· akalyaev
D83b63db79b42978fe35e120550ca180

Vim: Jump to gem source while using Vagrant

In Vim you can use tags to jump to the function declaration (or wherever you want). It is like "Goto declaration" feature in big IDEs (e.g. RubyMine). The problem is, when you are using Vagrant, you don't have access to gems sources because all setup is done inside the box, but you are running Vim outside of it. The solution I propose is to expose .rvm guest OS folder to the host OS.

Here I described the whole process from creating a directory to generating tags for the project.

I assume that you already know what ctags is (read this article if that is not the case).

1. Create ~/.vagrant_rvm directory on the host OS

$ mkdir ~/.vagrant_rvm

2. Expose .rvm guest OS folder to the host OS

Add this line to your project's Vagrantfile:

config.vm.synced_folder "/home/<username>/.vagrant_rvm", "/home/vagrant/.rvm"

3. Generate tags for your project from inside the guest OS

Install ctags on the guest OS (all major distributions have a package for ctags). Example for Ubuntu:

$ sudo apt-get install exuberant-ctags 

Run this command (assuming you are at the root of the project):

$ ctags -f gems.tags -R `bundle show --paths`

Next, we need to correct the paths so that they point to /home/<username>/.vagrant_rvm (not /home/vagrant/.rvm):

$ sed -i 's/\\/home\\/vagrant\\/\\.rvm/\\/home\\/<username>\\/\\.vagrant_rvm/' gems.tags

4. Check that you have this in your .vimrc

set tags+=gems.tags

You may also want to add gems.tags to the .gititgnore-global.

5. Give it a try!

Go to any gem's method and press Ctrl-].

P.S. If you know of a better solution, feel free to leave a link in the comments below and I'll delete this horrible set of hacks :)

Bonus: if you are using git, I would recommend to setup some git hooks, which will be (re)generating ctags every time you pull, commit or rebase (Thanks to Tim Pope).

Say Thanks
Respond