A better history command

The Bash history command lists your numbered command history -- all of it by default.  Other than limiting the output to the most recent N commands, it doesn't give you any real control over the output.  As a consequence I often find myself typing things like

 history | grep find

or even (I use find a lot)

 history | grep find | grep cpp

I found myself doing this so much that I eventually bit the bullet and wrote a better history command provides built in grep-like filtering.  So now I can type things like

 hist find
 hist find cpp
 hist find cpp viewsource

The last command corresponds to something like

 history | grep -i find | grep -i cpp | grep -i viewsource

which is a pain to type in full.

I've been using hist for a while now, and I've found it very useful, so I thought I'd share it in case there are other Bash users out there who might find it handy.  If you want to use it just paste the following into your .bash_profile:

 # Quick and dirty case-insensitive filtered history command.
 # "hist" ==> "history"
 # "hist foo" ==> "history | grep -i foo"
 # "hist foo bar" ==> "history | grep -i foo | grep -i bar"
 # etc.
 # Note that quotes are ignored, e.g.
 #   <<<hist "foo bar">>> is equivalent to <<<hist foo bar>>>
 hist()
 {
     HISTORYCMD="history $@"             # "foo bar" ==> "history foo bar"
     HISTORYCMD="${HISTORYCMD% }"        # "history " ==> "history" (no trailing space)
     eval "${HISTORYCMD// / | grep -i }" # "history foo bar" ==>
                                         #   "history | grep -i foo | grep -i bar"
 }
4 responses
Why not use reverse-search-history (CTRL-r)? It lets you do incremental partial searches of your history. http://www.faqs.org/docs/bashman/bashref_95.html
Well, first, I didn't know about Ctrl-R!

Second, I don't think Ctrl-R provides equivalent functionality to the multi-term filtering that the "hist" command gives you. So if you want to recall a find command over your C++ files -- the "hist find cpp" above, you can either search on the "find" or the "cpp" but not both.

There's a lot to know in the various shells. As for the functionality difference, for some reason, I remembered CTRL-r as behaving like Quicksilver's search. :) However, CTRL-r does search anywhere within the command. Also, press CTRL-r while searching to keep going backwards through matches.
ctrl-r is a KSH thing. There's also '!?string'. Check out my article "Advancing in the Bash Shell' for more of this kinda stuff.