A little ksh in my bash

On the OS X and Linux machines I manage, my default shell is bash.

This (for me) is really useful because:

  • bash is the default shell on OS X and Linux, so I don’t have to change anything in order to maintain my habits
  • I used bash from a lot of years and I’m pretty confident with the default shortcuts it provides

But, sometimes, I work on Solaris and AIX machines, which have the korn shell as default; so I learn some shortcuts for using that shell (especially in ‘’vi’’ mode). Finally, I find this shell a little boring, because I have to press more keys in order to achieve the same behaviour as bash (example: browsing the shell history are done with arrow keys in bash; with ksh and ‘‘vi’’ mode you must press ESC+k or ESC+j).

But there is a feature in the ksh which I love: the capability of editing the current line in a text editor, then executing it (aka ESC+v).

This is great, especially because I’m an automations lover, which means I write really long one-liner daily (a lot of times during each day, to be honest).

I ‘'’want’’’ this feature in my bash, so I searched and spended some hours trying to implement it.

First of all, unfortunately, you can’t do this with the (already available) plain vi editor. This is because vi locks the entire terminal, and you can’t piping the buffer out from the editor (without some explicit write command from the editor) .

But, there is a solution; you can use the ‘’’vipe’’’ command from moreutils package to piping your editor.

After installing this package:

$ brew install moreutils

You can just put the following bind in your ~/.bash_profile file:

bind '"\ev":"\C-aecho \047\C-e\047|vipe|bash\n"'

From now, when you have a really big one-liner you needs to edit in a more convenient way, you can press ESC-v and edit the command in vi. Remember, when you save and quit, the command will be executed immediately.

Enjoy

Written on February 11, 2015 - #Bash #Apple