Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

find previously entered commands (requires configuring .inputrc)

Terminal - find previously entered commands (requires configuring .inputrc)
<Meta-p> (aka <ALT+P>)
2013-09-10 17:13:02
User: hackerb9
9
find previously entered commands (requires configuring .inputrc)

[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' >> ~/.inputrc

echo '"\ep": history-search-backward' >> ~/.inputrc

bind -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. ☺)

Alternatives

There are 2 alternatives - vote for the best!

Terminal - Alternatives
<ctrl>+r
2013-09-08 13:49:19
User: zvyn
6

Searches bash-history in reverse order (last entered commands first). Pressing ctrl+r again shows the next matching entry.

tac ~/.bash_history | grep -w
2013-09-07 15:53:30
User: hamsolo474
Functions: grep tac
-3

greps your bash history for whatever you type in at the end returning it in reverse chronological order (most recent invocations first), should work on all distros.

works well as an alias

Know a better way?

If you can do better, submit your command here.

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

Comment by plasticphyte 52 weeks and 1 day 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.)

Comment by hackerb9 52 weeks and 1 day 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.

Comment by caesarsol 51 weeks and 6 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts