Quick tips are random posts regarding something I discovered on my way to something bigger. They usually get longer than “quick” would imply, for which I refuse to apologize.

I was playing around with some readline options this morning, after reading this post on Reddit Commandline. A few of them wouldn’t work for me, so I did some research into the version of Bash that comes with OS X.

Specifically, I wanted to get menu-complete-backward to work in my ~/.inputrc file. menu-complete is a cool feature where you bind a key combination and typing it cycles through available completions. It follows, then, that menu-complete-backward would cycle in the reverse order. It turns out you need a special escape sequence to pull this off on OS X.

Say you want to bind the Tab key to cycle completions one at a time instead of listing them all and requiring you to type characters until there’s only one match. In your .inputrc you would include:

TAB: menu-complete

Binding Shift-Tab to go backward is a little tricker:

"\e[Z": "\e-1\C-i"

The above is working perfectly for me in iTerm2 on GNU Bash 3.2.48(1)-release-(x86_64-apple-darwin11). It will take a few days to decide if I prefer this completion to the normal Tab-complete behavior or whether I want to bind this to a secondary key combination. Either way, it’s handy info to have.

While I’m on the subject, I was playing around with binding commands a while ago and added a couple to my .inputrc which I’ve found helpful. The following will bind Option-z to run cd - followed by ls, returning you to the previous directory in your stack and doing a file listing in one keystroke:

"\ez": 'cd -\015ls\015'

The \015 in the command is the return key, so it’s entering cd -, executing with return, then repeating with ls. You could add any parameters you want (ls -aF) if you don’t already have it aliased in your .bash_profile.

The next one binds Option-x to cd to the folder that was the last argument in my previous command. If I run cp myfile.txt ~/Desktop/, then hitting Option-X will take me to ~/Desktop/ and list its contents:

"\ex": 'cd !$\015ls\015'

Just some Terminal fun… to be clear, all of the above commands would go into the .inputrc file in your home folder. Create it if it doesn’t exist, and be sure to restart your bash shell before trying them out.