find previously entered commands (requires configuring .inputrc)

<Meta-p> (aka <ALT+P>)
[Click the "show sample output" link to see how to use this keystroke.]   Meta-p is one of my all time most used and most loved features of working at the command line. It's also one that surprisingly few people know about. To use it with bash (actually in any readline application), you'll need to add a couple lines to your .inputrc then have bash reread the .inputrc using the bind command:   echo '"\en": history-search-forward' >> ~/.inputrcecho '"\ep": history-search-backward' >> ~/.inputrcbind -f ~/.inputrc     I first learned about this feature in tcsh. When I switched over to bash about fifteen years ago, I had assumed I'd prefer ^R to search in reverse. Intuitively ^R seemed better since you could search for an argument instead of a command. I think that, like using a microkernel for the Hurd, it sounded so obviously right fifteen years ago, but that was only because the older way had benefits we hadn't known about.     I think many of you who use the command line as much as I do know that we can just be thinking about what results we want and our fingers will start typing the commands needed. I assume it's some sort of parallel processing going on with the linguistic part of the brain. Unfortunately, that parallelism doesn't seem to work (at least for me) with searching the history. I realize I can save myself typing using the history shortly after my fingers have already started "speaking". But, when I hit ^R in Bash, everything I've already typed gets ignored and I have to stop and think again about what I was doing. It's a small bump in the road but it can be annoying, especially for long-time command line users. Usually M-p is exactly what I need to save myself time and trouble.     If you use the command line a lot, please give Meta-p a try. You may be surprised how it frees your brain to process more smoothly in parallel. (Or maybe it won't. Post here and let me know either way. ☺)
Sample Output
### Start typing a command, say "nm", 
$ nm      # Realize you've done this before, so press M-p (hold ALT and hit P)
$ nm /usr/bin/wine       # Bash completes the line from history! Yay!
### Not the one you wanted? Just keep hitting M-p until you find the one you want.
$ nm /usr/bin/wineserver
### Need to be more specific? Note that the cursor is still after the "nm" that we typed at first, so if we hit "a" it shows this:
$ nma /usr/bin/wineserver
### ...but then, another press of M-p and, voila, we get:
$ nmap -sUT -p- localhost
### Just go ahead and hit enter (you don't need to be at the end of the line) and it runs:
Starting Nmap 7.21 ( ) at 2018-02-29 09:20 XGT
Interesting ports on localhost (
Not shown: 131045 closed ports
22/tcp    open          ssh
25/tcp    open          smtp
80/tcp    open          http

By: hackerb9
2013-09-10 17:13:02

2 Alternatives + Submit Alt

What Others Think

You can also add this to .inputrc to make the up/down arrow keys do a similar thing. ## Use already typed text to search through history - makes the history up/down keys more intelligent "\e[B": history-search-forward "\e[A": history-search-backward
plasticphyte · 464 weeks and 4 days ago
Oh, cool idea, plasticphyte! I actually always use Control-p and Control-n so my fingers don't have to leave the home row, so I'll try out your suggestion using this .inputrc:    "\C-n": history-search-forward  "\C-p": history-search-backward  "\e[B": history-search-forward  "\e[A": history-search-backward  "\eOB": history-search-forward  "\eOA": history-search-backward   Note the two extra escape sequences are ones that readline also recognizes as arrow keys by default. (Why readline doesn't just use terminfo instead of hardcoding such things is beyond me.)
hackerb9 · 464 weeks and 4 days ago
+1 for the up/down keys! i got used to this in gentoo linux and i always looked for this feature in other *nix.
caesarsol · 464 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: