sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)
Sophie ([personal profile] sophie) wrote in [community profile] command_liners2010-08-03 10:35 pm
Entry tags:

bash history search

A quick tip for bash users that I use all the time:

If you have a command in your history that you want to re-execute or edit, but don't want to keep tapping the up key, hit Ctrl-R and then start typing part of the line. bash will find the last line matching your input on the fly. If it's not the one you want, either keep typing until it is, or hit Ctrl-R and bash will show you the next match.

Once you've got it, you have two options: to execute, you can then just press RETURN; to edit, use a movement key (left/right arrows, Home/End, etc) and bash will drop you back to a standard command line with the line you just found.

This trick also works if you know the middle of the line but not the beginning, since bash searches the entire line.

[edited to add: BTW, Fey, do you realise that nobody can add new tags to posts in this community? The only tags in here are ones first used by you. :)]
foxfirefey: Fox stealing an egg. (mischief)

[personal profile] foxfirefey 2010-08-03 09:47 pm (UTC)(link)
FIXED! Thanks for the reminder!
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2010-08-04 02:14 am (UTC)(link)
Oh nice; I use Ctrl-R, enter a lot, but I wasn't aware that you could just edit. Nifty!
vass: Small turtle with green leaf in its mouth (Default)

[personal profile] vass 2010-08-04 02:29 am (UTC)(link)
That's so cool!
jld: “0wned!!!” (0wned)

[personal profile] jld 2010-08-04 05:35 am (UTC)(link)
The other fun thing with C-r is that it works within a line. So if you want to do the last command but change the part after the last /, then it's up C-r / C-k and then type… oh, huh. That also works without the up-arrow, I guess. I'll have to remember that.

Because for history search I've become a big fan of using zsh's tab-completion, which does everything, to expand csh-style history references. And then C-_ to undo and fix it if it gets the wrong thing. So it's gotten to the point that I only rarely use C-r for that. (And I got started with emacs and bash, so I used to use it a lot, too.)
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)

[personal profile] pne 2010-08-04 12:16 pm (UTC)(link)
And in ksh with vi bindings, ESC /.

Though I imagine not many people who read this use that, but it was what I had at work for many years on a HP-UX machine.