Running Vim within IRB
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.
I’ll be running my Core Vim Class online on Thursday, December 5th. Tickets cost $255, but you can get the earlybird discount of $230 if you buy yours before November 29th. The price includes an exclusive screencast that summarises the material from the class.
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.
Install the interactive editor gem by running this at the command line:
gem install interactive_editor
~/.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
utility_belt gems in your .irbrc file, there may be some conflict
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.