Home » Search for a previous command with the prefix I just typed

Search for a previous command with the prefix I just typed

Solutons:


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.

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.

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.

Related Solutions

Joining bash arguments into single string with spaces

[*] I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all: str="'$*'" $* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space....

AddTransient, AddScoped and AddSingleton Services Differences

TL;DR Transient objects are always different; a new instance is provided to every controller and every service. Scoped objects are the same within a request, but different across different requests. Singleton objects are the same for every object and every...

How to download package not install it with apt-get command?

Use --download-only: sudo apt-get install --download-only pppoe This will download pppoe and any dependencies you need, and place them in /var/cache/apt/archives. That way a subsequent apt-get install pppoe will be able to complete without any extra downloads....

What defines the maximum size for a command single argument?

Answers Definitely not a bug. The parameter which defines the maximum size for one argument is MAX_ARG_STRLEN. There is no documentation for this parameter other than the comments in binfmts.h: /* * These are the maximum length and maximum number of strings...

Bulk rename, change prefix

I'd say the simplest it to just use the rename command which is common on many Linux distributions. There are two common versions of this command so check its man page to find which one you have: ## rename from Perl (common in Debian systems -- Ubuntu, Mint,...

Output from ls has newlines but displays on a single line. Why?

When you pipe the output, ls acts differently. This fact is hidden away in the info documentation: If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is...

mv: Move file only if destination does not exist

mv -vn file1 file2. This command will do what you want. You can skip -v if you want. -v makes it verbose - mv will tell you that it moved file if it moves it(useful, since there is possibility that file will not be moved) -n moves only if file2 does not exist....

Is it possible to store and query JSON in SQLite?

SQLite 3.9 introduced a new extension (JSON1) that allows you to easily work with JSON data . Also, it introduced support for indexes on expressions, which (in my understanding) should allow you to define indexes on your JSON data as well. PostgreSQL has some...

Combining tail && journalctl

You could use: journalctl -u service-name -f -f, --follow Show only the most recent journal entries, and continuously print new entries as they are appended to the journal. Here I've added "service-name" to distinguish this answer from others; you substitute...

how can shellshock be exploited over SSH?

One example where this can be exploited is on servers with an authorized_keys forced command. When adding an entry to ~/.ssh/authorized_keys, you can prefix the line with command="foo" to force foo to be run any time that ssh public key is used. With this...

Why doesn’t the tilde (~) expand inside double quotes?

The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal. POSIX defines Double-Quotes as: Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the...

What is GNU Info for?

GNU Info was designed to offer documentation that was comprehensive, hyperlinked, and possible to output to multiple formats. Man pages were available, and they were great at providing printed output. However, they were designed such that each man page had a...

Set systemd service to execute after fstab mount

a CIFS network location is mounted via /etc/fstab to /mnt/ on boot-up. No, it is not. Get this right, and the rest falls into place naturally. The mount is handled by a (generated) systemd mount unit that will be named something like mnt-wibble.mount. You can...

Merge two video clips into one, placing them next to each other

To be honest, using the accepted answer resulted in a lot of dropped frames for me. However, using the hstack filter_complex produced perfectly fluid output: ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack output.mp4 ffmpeg -i input1.mp4 -i input2.mp4...

How portable are /dev/stdin, /dev/stdout and /dev/stderr?

It's been available on Linux back into its prehistory. It is not POSIX, although many actual shells (including AT&T ksh and bash) will simulate it if it's not present in the OS; note that this simulation only works at the shell level (i.e. redirection or...

How can I increase the number of inodes in an ext4 filesystem?

It seems that you have a lot more files than normal expectation. I don't know whether there is a solution to change the inode table size dynamically. I'm afraid that you need to back-up your data, and create new filesystem, and restore your data. To create new...

Why doesn’t cp have a progress bar like wget?

The tradition in unix tools is to display messages only if something goes wrong. I think this is both for design and practical reasons. The design is intended to make it obvious when something goes wrong: you get an error message, and it's not drowned in...