Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Running Vim within IRB

#20

Run time:

If you work with ruby you will know that the interactive ruby shell, or ‘IRB’ for short, is a useful sketchpad for coding. But the command line interface of IRB can feel quite limiting in comparison with the power of your text editor. In this episode, I’m going to demonstrate how you can get the best of both worlds, by loading Vim from inside IRB.

Shownotes

IRB is great for trying out one liners, but if you need to sketch longer blocks of ruby, it soon falls down.

In his Utility Belt gem, Giles Bowkett has collected a grab-bag of tricks and techniques for IRB, the highlight of which is the ability to interactively edit code in your text editor. The Utility Belt gem hasn’t been updated in a little while, but the interactive editor has continued to evolve, thanks to Jan Berkel and with help from Charles Nutter.

Installation

Install the interactive editor gem by running this at the command line:

gem install interactive_editor

Create an ~/.irbrc file if you don’t already have one, then paste the following into it:

require 'rubygems'
require 'interactive_editor'

Note that the utility belt gem also includes an interactive editor, but it is currently out of date. If you require both interactive_editor and utility_belt gems in your .irbrc file, there may be some conflict between them.

Vim: restore cursor position and highlight syntax

To get the most from this technique, you will want to ensure that you have enabled filetype detection and syntax highlighting in Vim. Additionally, it is helpful to have Vim restore the cursor position to where you left it. The following snippet of Vimscript takes care of these.

if has("autocmd")
  " Enable filetype detection
  filetype plugin indent on

  " Restore cursor position
  autocmd BufReadPost *
    \ if line("'\"") > 1 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif
endif
if &t_Co > 2 || has("gui_running")
  " Enable syntax highlighting
  syntax on
endif

Note that the example vimrc file that comes with Vim includes all of the above, so don’t paste this into your vimrc without first checking if you already have this functionality.

Comments

Browse similar content


Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..