«

Vi Bindings in Node Repl

I fell in love with Vim about three or so years ago and now I prefer to have vi bindings whenever I edit text. Luckily, I’m not alone on that front and many developers have created extensions and utilities to keep our hands safely on the home row. There’s Vimium for browsing in Chrome, a vi mode for ZLE the zsh line editor (as well as readline for BASH and other shells) and even Zathura for the rare occasions where I need to view PDF files.

I frequently hop into the NodeJS REPL to quickly test out code or to do demonstrations of alternate approaches to my colleagues. The Node REPL includes it’s own bespoke version of readline but it only supports the emacs mode.

That’s where “rlwrap” comes in handy. It’s a wrapper for the GNU readline utility that allows you to use readline with applications that don’t natively support it. I installed it (pacman -S rlwrap for Arch and brew install rlwrap for MacOS) and then added an alias to my collection of shell functions:

if type rlwrap > /dev/null; then
  function vi-node() {
    NODE_NO_READLINE=1 EDITOR=vi rlwrap node
  }
fi

When I run vi-node I get a Node REPL with all of the modal editing goodness I could want. This is all covered in the Node REPL documentation.

Their are two downsides that I’ve run into. First, the .editor REPL function which takes you into editor mode doesn’t seem to work inside rlwrap. That’s not a big deal for me as I only use the REPL for tiny pieces of code. What may be a bigger downside for some people is that the autocomplete functionality of the REPL doesn’t seem to work under rlwrap. Then again, that functionality isn’t very useful as implemented anyway.

Now if only there was a way to use vim bindings in Google Docs!