Fun with inputrc, part 1
If you’re using Linux or any other UNIX, the following should work just fine & dandy. If you use a Mac, you need to perform an extra step. Mac OS X doesn’t ship with any inputrc
file, either at /etc/inputrc
or ~/.inputrc
. First let’s create one, at ~/.inputrc
& then open it with your favorite text editor & add the following (or, open your text editor, add the following, & save it to ~/.inputrc
):
# Use Unicode & do NOT use the "8bit hack" to input/output non-ASCII characters
# See http://code.google.com/p/iterm2/wiki/Keybindings
set input-meta on
set output-meta on
set convert-meta off
# When pressing up or down arrows,
# show only history entries that match what was already typed
"\e[A":history-search-backward
"\e[B":history-search-forward
# Turn on case insensitivity for tab-completion
# Ex.: type "doc<tab>" to search for "Documents"
set completion-ignore-case On
The first group is easy: use Unicode.
The second is a bit more complex, but is crazily useful. The history
command is a thing of beauty. Every command I type is saved, so I can always go back & see what I’ve typed or re-run a command.
If I press Ctrl-r
, I can search through my history, as you can see in the following, in which I show you what happens as I type each letter while I search my history for grep
:
$ [press Ctrl-r]
(reverse-i-search)`g': ping www.google.com
(reverse-i-search)`gr': alias | grep hist
(reverse-i-search)`gre': alias | grep hist
(reverse-i-search)`grep': alias | grep hist
Now if I press Enter1, that command runs: alias | grep hist
.
What if I want to find other commands in my history that used grep
? After I type out grep
& find one, just keep pressing Ctrl-r
to cycle back:
(reverse-i-search)`': /Users/rsgranne/bin/Web/list-urls.py http://io9.com/5907828/what-if-edward-gorey-drew-lovecrafts-unspeakable-horrors | grep jpg
(reverse-i-search)`grep': for i in $(/Users/rsgranne/bin/Web/list-urls.py http://io9.com/5907828/what-if-edward-gorey-drew-lovecrafts-unspeakable-horrors | grep jpg) ; do wget $i ; done
(reverse-i-search)`grep': ps aux | grep Stat
(reverse-i-search)`grep': grep utf8 ~/.vimrc
And so on.
If I’m just sitting at the prompt & type nothing & press the Up arrow, I go back through my command history one line at a time; if I press the down arrow, I go forward one line at a time. Press Enter, & the previous command runs. The problem is that this is often inefficient. Enter these two lines in ~/.inputrc
:
"\e[A":history-search-backward
"\e[B":history-search-forward
Now if I type a few letters of a command & press the Up arrow, the only commands that show up are those that contain the letters I just typed. Same thing for the down arrow. Like this (each of the following is on the same line, replacing the previous one; I’m just showing the commands on multiple lines so it’s obvious what’s happening):
$ ls[press Up arrow]
$ ls ~[press Up arrow]
$ ls /etc[press Up arrow]
$ ls /usr/local/lib
And so on.
The third one means that I can use tab completion & not have to worry about capitalization. Normally, if this wasn’t enabled, I’d get this:
$ ls -1
Applications/
Desktop/
Documents/
Downloads/
Dropbox/
…
$ cd doc[Tab]
…aaaaand nothing except a beep. I’m telling bash
to cd
into a directory beginning with doc
, & there isn’t a directory that begins with doc
. There’s one that starts with Doc
, but that’s not matched.
If this line is in ~/.inputrc
, things will change:
set completion-ignore-case On
And now I get:
$ ls -1
Applications/
Desktop/
Documents/
Downloads/
Dropbox/
…
$ cd doc[Tab]Documents
Kinda hard to see in text like this, but as soon as I press Tab, doc
expands to Documents
. In other words, I can use tab completion without having to worry about capitalization, which is great for lazy people like me.
There’s more you can add in ~/.inputrc
, but those are a good start. Try them, get used to them, & then come back for more.
-
Yes, I could have stopped after typing
gr
, since the most recent command withgrep
in it was found. But I wanted you to see the whole thing. ↩