Launching irb from vim

You can run irb from vim with the command !irb.

Running !irb -r % should run the current file in irb then leave irb open. Unfortunately ruby 1.9.2 removed the current directory from the load path which causes this to fail. You could construct a path but it is easier to add the current directory back in, especially if you are using irbtools which does it for you.

Once irbtools are installed add:

map <F2> <ESC>:w<CR>:!irb -r %<CR>

to .vimrc.local which will write then load the current file into irb.

Edit: I was getting some weirdness with the irb prompt with irbtools so I have removed it and added

$: << ‘.’  if RUBY_VERSION >= ‘1.9.2’

to my .irbrc file to add the current directory to the load path.

 

One thought on “Launching irb from vim

  1. I love this, I use vim a ton when making small command line tools. Fun fact! This doesn’t work if you name your script without the .rb extension, because the -r (like the require method) itself then appends .rb and won’t find it. You may also need to use ./% instead of %, depending on how you invoke vim.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.