Live in-place Ruby execution

Today I was able to get Vim to execute Ruby files without ever having to leave Vim, and putting the results of execution in place next to the code that was executed. Here’s a quick demo on Youtube.

There’s three parts to this:

The rcodetools gem, which includes the CLI tool xmpfilter, which does the work.
The xmpfilter vim plugin which hooks up Vim to xmpfilter.
The relevant mappings in your vimrc to get it all working.
Installing the gem is simple:

gem install rcodetools

You then need to install the xmpfilter vim plugin. I recommend going down the Pathogen route for installing plugins, but do it whichever way you prefer.

There’s two parts to marking a line for execution. The first is to comment it with the # => syntax, which is what xmpfilter looks for. The second is then a command to run xmpfilter over the current file.

Here are the mappings I’ve gone for:

nmap <buffer<F4<Plug(xmpfilter-run)
xmap <buffer<F4<Plug(xmpfilter-run)
imap <buffer<F4<Plug(xmpfilter-run)
nmap <buffer<F3<Plug(xmpfilter-mark)
xmap <buffer<F3<Plug(xmpfilter-mark)
imap <buffer<F3<Plug(xmpfilter-mark)

Now I can hit F3 to mark a line to execute and then F4 to execute it.

Note: if you use rbenv to manage your Ruby versions like I do, you’ll need Tim Pope’s rbenv.vim plugin which tells Vim where your Ruby is located. Without this plugin I couldn’t get xmpfilter to work, presumably due to not being able to locate the rbenv installed version of Ruby.… Click Here

Vim’s `lcd` command

In Vim you can use :cd to change directory across your Vim session. But did you know you can use lcd to change directory for the current window?

I find this useful if I’m in one project and need to dig into some code from another. I will open a new tab and then lcd into place. Once I’m done I can then delete that tab and in my other window(s) I’m still in the directory I was earlier.

See :help lcd for more.

comments powered by Disqus… Click Here

Binding Caps-lock to Control and Escape

As a Vim user, the ESC key is an important one. To do Vim the proper way you’re constantly switching in and out of insert mode. The escape key, however, is a fair distance from the fingertips of most typists. The caps-lock key is the polar opposite – it’s rarely useful, yet in a prominent position on the keyboard.

Many developers rebind their caps-lock key to control. Control is used fairly often (particularly for those spending hours in a shell), but sometimes awkward to reach, especially on laptop keyborads.

On Mac OS X, using the aptly-named KeyRemap4MacBook, you can solve these two problems in one by binding the caps-lock key to escape if pressed alone, or to control if pressed in combination with another key.

In OS X’s keyboard settings, bind caps-lock to control:

Then, in KeyRemap4MacBook’s preferences, enable this setting:

This post was contributed by @mark_js.

comments powered by Disqus… Click Here