Search for a previous command with the prefix I just typed

We can use up and down arrow to navigate in command history.

In some IDEs, such as Matlab, if we input something and then press the arrow keys, we scroll among only the history commands starting with what we have input. That’s really convenient, but in a shell terminal, this doesn’t work.

Is there some way to gain a similar function in a shell terminal? And any other tips for improving efficiency in terminal use?

Asked By: wsdzbm

||

What you are looking for is CtrlR.

Type CtrlR 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 CtrlS instead. (If CtrlS 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.

Answered By: John1024

The means I usually use is to combine the history command with grep

IE:

history | grep <command im looking for>

That will display a numbered history of commands you have typed that contain that command, you can then use:

!<number listed in history command>

to redo that command.

IE:

history | grep history

142  history
143  history | grep lvresize
568  history | grep echo
570  history | grep echo
571  history | grep history

followed by:
!571

will repeat history | grep history

Answered By: Gravy

Place these in your ~/.inputrc:

"e[5~": history-search-backward
"e[6~": history-search-forward

These make Page Up and Page Down behave as you wish. (Some distributions have it already configured it for you.) I personally find these way more convenient than Ctrl+R or history.

Update: bash-5.2 / readline-8.2, released in Sep 2022, make these the default behavior. No more need to tamper with inputrc to set it up.

Answered By: egmont

I always tend to configure my machines with an large HISTSIZE value so it keeps a longer history list, as well as HISTTIMEFORMAT with the time stamp value so I can see when was the command ran.

export HISTSIZE=10000
export HISTTIMEFORMAT="%m/%d/%Y %T "
Answered By: Alpha01

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 foo, cd 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 -l, less, and anything else you do a lot. (cp -i, mv -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.

Answered By: Peter Cordes

If it’s okay for you to use zsh instead of bash, you can use oh-mi-zsh for that.

You can navigate your command history with the up and down arrows. It also allows the same navigation when you have already typed the text (as in Mathlab).

edit: Oh-my-zsh is a large, developer-oriented software package that includes many other features, e.g. aliases, themes, plugins. These extras make the distribution more complex to use than a keyboard shortcut.

https://github.com/robbyrussell/oh-my-zsh

Answered By: user9869932

Add the following alias to ~/.bashrc

alias h='history | awk '"'"'{$1="";print}'"'"' |sed "s/^ *//" |grep -v "^h " | sort | uniq | grep'

Usage: h fire will get me a list of commands contain fire. or h ^fire will get me a list of commands begin with fire.

This is useful when I want to view a series of commands at the same time.

By the way, '"'"' is ugly but I couldn’t find other ways to avoid single quotes in single quotes.

Answered By: Hosi Golden

Add the following lines to your ~/.inputrc

"e[A": history-search-backward
"e[B": history-search-forward

This is similar to egmont’s answer, but instead of using Page Up and Page Down, it uses the escape sequences for Arrow Up and Arrow Down. This way is much more convenient to use on a Mac.

Answered By: John Slavick

I used daily these tips (and some others) in my bash terminal:

  • Execute last command: !!

  • Print last command: !!:p (It’s equal to press up arrow [])

  • Execute command N from history: !N

  • Print command N from history: !N:p

  • Execute last command starting with prefix from history: !prefix

  • Print last command starting with prefix from history: !prefix:p

Answered By: Nolwennig
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.