The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,465 other followers

bash – Search for a previous command with the prefix I just typed – Unix & Linux Stack Exchange

Posted by jpluimers on 2021/08/18

[WayBack] bash – Search for a previous command with the prefix I just typed – Unix & Linux Stack Exchange answered by [WayBack] John1024:

What you are looking for is Ctrl-R.

Type Ctrl-R and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous matching commands.

To search backwards in the history, type Ctrl-S instead. (If Ctrl-S doesn’t work that way for you, that likely means that you need to disable XON/XOFF flow control: to do that, run stty -ixon.)

This is documented under “Searching” in man bash.

Comment by [WayBack] HongboZhu:

Ctrl-Q to quit the frozen state, if you already hit Ctrl-S without turning off flow control first and got your terminal frozen.

A far more elaborate answer with many other tips is from [WayBack] Peter Cordes:

Besides ^r / ^s history i-search:

alt. inserts the last “word” of the previous command at the cursor. Repeat it to get the last word from older commands. (But note that a trailing & counts as the last word for background commands).

This is super handy for mkdir foocd alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd.

To get the 2nd-to-last word, use esc - 2 alt+. (i.e. use an emacs-style numeric argument to alt+.. Negative counts in from the end, positive counts forward from the start.) But this is usually more trouble than it’s worth; at some point it’s faster to reach for the mouse and copy/paste, or up-arrow and ^w / ^y part of it (see below).


If your terminal is set up nicely/properly, ctrl-left and ctrl-right will go backward/forward by words. If not, hopefully at least alt-b and alt-f will do the same thing.


ctrl-/ is an undo. You can use auto-repeat for deleting words much more efficiently if you can undo when you overshoot by a bit.


More powerful mixing/matching of commands comes from using the kill-ring, which works just like in Emacs. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text.

Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.

Combining all of these together, you can

  • start typing a command
  • ctrl-r to go back to an old command and grab part of it with control-w or similar.
  • esc-r or alt+r to restore it to how it was before you deleted part of it.
  • alt-> to go to the end of history (i.e. down-arrow all the way), to get back to the command you were in the middle of.

Other interactive-use tips:

Enable shopt -s globstar, so you can do **/*.c (recursive including the current dir). Sometimes handy for interactive use, but usually find -name '*.c' -exec foo {} + is better.

If you write bash scripts, you’ll find it handy to have shopt -s extglob enabled in your interactive shells, too. You will sometimes find a use for stuff like *.!(c|h) to match files that don’t end with .c or .h.

Find aliases you like for ls -lless, and anything else you do a lot. (cp -imv -i, and rm -I are nice. Don’t get in the habit of depending on them to do a selective rm. GNU rm’s -I asks once for all the args.)

I like alias m=less (m for “more”). I have less set up with , and . bound to previous / next file (lesskey). The default is a multi-keypress sequence that can’t be used with autorepeat.


I like to do everything inside GNU screen. I find it easier to keep track of numbered screen-windows than a boatload of tabs in Konsole (or any other terminal emulator I’ve tried). If you don’t already know screen, learn tmux because it’s newer and less crufty.

To get something like the functionality of opening a new shell with the same cwd as another shell, I use a custom hook for cd/pushd/popd that lets me do cds 8 to cd to whatever dir my shell in screen window 8 is using. This works even for shells outside of the screen session, as long as there’s only one screen session.

I searched for the above because of the below shortcuts video: recommended watching!

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: