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 ( http://nmap.org ) at 2018-02-29 09:20 XGT
Interesting ports on localhost (127.0.0.1):
Not shown: 131045 closed ports
PORT      STATE         SERVICE
22/tcp    open          ssh
25/tcp    open          smtp
80/tcp    open          http
[...]

9
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 · 479 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 · 479 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 · 479 weeks and 2 days ago
Amazing! This blog looks just like my old one! It's on a completely different subject but it has pretty much the same layout and design. Wonderful choice of colors! Pug Puppies for Sale Near Me PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under $500 Near Me pug puppies for sale in texas pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio
rahimhh21 · 13 weeks and 3 days ago
Pug Puppies for Sale Near Me pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under $500 Near Me pug puppies for sale in texas pug puppies for sale $200 pugs for sale near me under $500 pugs for sale under $400 near me pugs for sale near me puppies for sale near me under $500 pug puppies for sale under $1,000 near me pug for sale pug puppies for sale under $300 Brindle Pug Pitbull Pug Mix Pugs for sale cheap Cheap pug affordable pug puppies for sale near me black pugs for sale near me White Pugs for sale pug dog for sale free pug puppies pug puppies for sale in my area mn pug breeders pug puppies indiana pugs for sale michigan PUG PUPPY ADOPTION Pug puppies for sale Pug puppies for sale near me Pug puppies near me Pug Puppies for Sale Under $500 Near Me Cute Pug Puppies Black pug puppies Black pug puppies for sale pug puppies for adoption black pug puppies for sale near me chihuahua pug mix puppies how much is a pug puppy teacup pug puppies baby pug puppies baby pug puppies for sale pictures of pug puppies pug puppies for sale in Ohio pug puppies price pug mix puppies teacup pug puppies for sale best food for pug puppy newborn pug puppies pug puppies craigslist pug puppies for sale craigslist adorable pug puppies how much does a pug puppy cost Pitbull pug mix puppies pug pit mix puppy pug puppies for sale $200 pug puppies for sale in NJ Pug puppies for sale in Wisconsin pug puppy cost pug puppy food royal canin pug puppy royal canin pug puppy food fawn pug puppy pug puppies for sale florida pug puppies for sale in Indiana pug puppies for sale in KY pug puppies for sale in NC pug dog puppy AKC Registered Pug Puppies For sale cheap pug puppies for sale near me cheap pug puppies for sale in California cheap pug puppies for sale in nj Black Pug Puppies for sale pugs puppies for sale
Perfecthomepugs · 4 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: