oqtj8w

The single most useful thing in bash

Create ~/.inputrc and fill it with this:

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

This allows you to search through your history using the up and down arrows … i.e. type "cd" and press the up arrow and you'll search through everything in your history that starts with "cd".

It's a little bit like ctrl-r (mentioned in many of the comments below), but anchored to the start of the line, and the arrow keys allow you to scroll back and forth between matches.

I use it when I'm looking to (for instance) call up the last ping I did (hit p, up arrow, return), whereas I use ctrl-r more like search, when I'm trying to find a command based on an argument or option that I used.

Both useful.

Other options that I find useful to add:

set show-all-if-ambiguous on

This alters the default behavior of the completion functions. If set to ‘on’, words which have more than one possible completion cause the matches to be listed immediately instead of ringing the bell. The default value is ‘off’.

set completion-ignore-case on

If set to ‘on’, Readline performs filename matching and completion in a case-insensitive fashion. The default value is ‘off’.

(as miah points out below, this is all actually Readline functionality. The title should be "Readline is the single most useful thing in everything" ;)

63 Responses
Add your response

1404
Tiagox.in

I'll start to use it.

over 1 year ago ·
1447
Photo on 08.01.2013 at 04.15

install zsh ;)

over 1 year ago ·
1646
Be0473b0966b7766822d4316c6bf9106

I guess I'm crazy enough to use fish that contains this feature by default ;-).

over 1 year ago ·
1785

Or just use Ctrl-R !

over 1 year ago ·
1788
Thumb alyssa pohahau

as @matleclaire said, just use ctrl-r!

over 1 year ago ·
1790
Yo

it's the pro version of "history | grep putyourcommandhere" :D thank you!

over 1 year ago ·
1794
35cc187075b514ffdbcc6b006b69714c

i think it is a middle term solution between CTRL+R and ZSH!

;)

over 1 year ago ·
2245
4ff020826d61a6cfd875cb5cc29e5ce6

This is better than [CTRL]+[R].

over 1 year ago ·
2279
7051e1e8408de9dbb72eedb0a900098f

Also consider "set -o vi", for a set of vi powertools at the command line. Search history with regexes, traverse lines with vi keybindings, etc.

over 1 year ago ·
2431
8b13e08e2eb0886384acfb8b46a90963

Using the arrow keys is almost as bad as using a mouse, I try to leave the homerow as little as possible. So I would just type 'cd /' then hit ctrl-r repeatedly until I find the command I want. Or more likely the first few characters of the directory I want to cd into and hit ctrl-r a few times until I have the command I want.

over 1 year ago ·
2498
Gavatar big

ALSO: This makes your bash complete case INsensitive... Just because nobody mentioned it explicitly(except the command itself ;).
And CTRL-R is not even close to what this does, honestly... try first.

over 1 year ago ·
2499
Gavatar big

@matleclaire this is not even close to CTRL-R.
What about case insensitive completion? At least on my machine CTRL-R is not helping there. Also, multiple CTRL-R are also not behaving like this.

over 1 year ago ·
2670
891d598f28745d268ec3cac10390045a

This is better than ctrl + r and can be used together with zsh. win + win.

over 1 year ago ·
2748
2e1e340e378a26f2c69c6dd418060374

If you want to apply this globally put it in /etc/inputrc. On debian/ubuntu the file is already present, just un-comment the line with '"\e[5~": history-search-backward' etc and check the other nice options!

over 1 year ago ·
3451
44ba5ab0ceceafd8b79c5ca236f16b2a

This is awesome!

Solved my long lasting confusion. I know ctrl+r but seems I can not search forward if I accidentally missed the command line I'm searching for. The arrow keys are more friendly.

over 1 year ago ·
4139
Yo nueva

Nice. I've been using this feature since I've switched to ZSH + Oh-my-zsh, where it comes enabled by default.

over 1 year ago ·
4720
591564c63c9b8b21f9c13cbddb7c000a

Huh! On bash it is same as using PageUp and PageDown. Type cd / and do PageUp.

over 1 year ago ·
5112
Jasdeepkhalsa

@moiseevigor I was going to say, on Ubuntu something similar already seems to be in place! How does this differ?

over 1 year ago ·
5666
C48498c82bfaf0c6c8843ba85e3cd598

Personally I find this annoying. At times I'll (for whatever reason) already have something typed into the prompt, and I realize I need a command from recent history, so I press up a few times. In this case I'd first have to press ctrl+a and ctrl+k to kill what I've already typed, or things would get very strange.

It's a cool feature, but it does come down to personal preference, and personally, ctrl+r is more than good enough.

It might be worth mentioning though that I use ZSH and oh-my-zsh.

over 1 year ago ·
5667
Me

@glitchmr not only you :)

over 1 year ago ·
5668
Default profile 2 normal

zsh is for hipsters :-P

over 1 year ago ·
5671
522f44265e0ed80578f6d3af4cdf7011
over 1 year ago ·
5673
475ee632ba6d035031c993ea37caf469

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

+

zsh history-substring-search plugin

= awesomeness in ZSH

over 1 year ago ·
5674
Photo on 08.01.2013 at 04.15

@ipetepete Actually I prefer antigen, and enhancing zsh by myself :)

over 1 year ago ·
5675
Bcb2185dd24051bce727b29230a2c171

awesome tip .

over 1 year ago ·
5676
Ab4ba0d9e6edcd2ba1564006a748fe01

@glitchmr I use fish as well. I feel that lots of people using zsh would love fish if they tried it.

over 1 year ago ·
5678
32084881f99b9d0779df4555b0c9309e

This actually has nothing to do with bash and has more to do with readline. So long as your shell supports readline these features should be available.

http://cnswww.cns.cwru.edu/php/chet/readline/readline.html

over 1 year ago ·
5679
Nois

awesome!

over 1 year ago ·
5680

Or just use :
alias hs="history | grep"
Save it to your .bashrc file to make it permanent.

over 1 year ago ·
5683
54e2ad6fd33097654a99a61818f61e0a

mind = blow

over 1 year ago ·
5684
Default profile 2 normal

OhMyZsh! is super-hipster!

over 1 year ago ·
5686
71a3e2556ab7c41a1331422282efddf4

you should really check out fish (http://ridiculousfish.com/shell/)

over 1 year ago ·
5689
3b10d886cd4d04d9f5e1e559d4cfd1a9

Awesome!

over 1 year ago ·
5690
E780b272998a583d291a9897dc576132

I prefer "\ep" for history-search-backward and "\en" history-search-forward. In this way, C-p retrieves the previous command from history and M-p retrieves the previous matching command from history. It makes more sense to me.

over 1 year ago ·
5691
C62eab16ab95c0b8da5a5a7592fc2a3e

I used to use fish, but switched to zsh (with oh-my-zsh). The backwards compatibility does wind up making life easier and there aren't many fish features I miss.

over 1 year ago ·
5700
C3338e2c4d21e356449c00842c4d3ed9

Step 1: Install https://github.com/robbyrussell/oh-my-zsh

Step 2: Edit your ~/.zshrc, find the "plugins" line, and add "history-substring-search" to the end. Here's mine:

$ grep "plugins=" ~/.zshrc
plugins=(git … history-substring-search) 
over 1 year ago ·
5709
Bnw

Yeah. Start using zsh and then make your dotfiles better.

over 1 year ago ·
5711
591564c63c9b8b21f9c13cbddb7c000a

The problem with these configuration is it prints ;5D and ;5C when CTRL+<Left> and CTRL+<Right> are pressed instead of moving cursor to one word left and right.

over 1 year ago ·
5717
B320a570028e58a10abedd141ee63668

hey, nice tip, but the cursor keeps its position at the beginng, how can I set to make the cursor locate at the end of the command when moving backwd/fwd? thanks

over 1 year ago ·
5749
048c28e0adfd7ae755b6035c6db0d6ec

I already have the show-all-if-ambiguous and completion-ignore-case enabled in my input.rc and ctrl+r takes benefit of that.

One thing ctrl+r does differently is that you can search in the middle of your command (and not just the beginning), which I find more useful.

For example, I have way too many commands that start with cd, but if I use ctrl+r and type in the unique part of the filepath that I want, it jumps straight to the command I'm looking for.

over 1 year ago ·
5759

When you want to keep your Home and End keys to work, add:

"\e[1~": beginning-of-line

"\e[4~": end-of-line

"\e[7~": beginning-of-line

"\e[8~": end-of-line

"\eOH": beginning-of-line

"\eOF": end-of-line

"\e[H": beginning-of-line

"\e[F": end-of-line

over 1 year ago ·
5777
Eea09ae62533e44d90e78edfd64a43d2

The first two lines are actually useful. But the last two things will get very annoying very fast, specially if you're used to working with a Unix-type CLI often.

over 1 year ago ·
5797

If you want CTRL+<right> and CTRL+<left> to continue working, you'll need these changes as well:

"\e[1;5C": forward-word

"\e[1;5D": backward-word

over 1 year ago ·
6097

just get guake....

over 1 year ago ·
6714

This was very very util ! thanks!! :D
I'm using this "bash shortcut" in my Linode, it's excellent! Thanks again

over 1 year ago ·
6823
Bbccad8b762ed44688e2f0090826baf9

no way ... it is awesome

over 1 year ago ·
8321
Bcecdef24aac34873f7a5e7dec8a3e00

Neat!

over 1 year ago ·
8401
781f1c414c26ceb958de5dd665ebbb7e

I can understand people's love for zsh, but it doesn't help people who use Bash and just want to improve it.

two cents

over 1 year ago ·
10191
0 uihhf6vabsacjihhu6icf54 fpomuirhmgpbf5iy4soyk7fecl4xtlxtdael42axsho9hgzhkotl

works on bash
For csh use
bindkey -k up history-search-backward
bindkey -k down history-search-forward

over 1 year ago ·
10301
520a0b621575028b78321af64d34108e

Thanks for the tip, but I like my ups and downs the way they are. Never used ctl+r but will now!

over 1 year ago ·
10732
8fdfe6510f04a881be959b60c1fdbb21

Good one

over 1 year ago ·
10806
E8 normal

-bash: \e[A:: command not found

on OS X, what's wrong?

over 1 year ago ·
11861
54bc1d2b746169ee506b553b218b18aa

thats totally awesome!

over 1 year ago ·
11862
54bc1d2b746169ee506b553b218b18aa

thats totally awesome!

over 1 year ago ·
12895
My face

thanks for sharing man

over 1 year ago ·
13515
0 zsvrnl4h8cuhwoey9ofhnbvhutd7woryvutknbwvpkjxns2oqw5c4fjucfskojvtmmblmhbjmeom

awsome man..

over 1 year ago ·
13736
Morton 066e

Its a good tip, but yeah - zsh has this and soooo much more.

over 1 year ago ·
15345
8e9e135bb9e8b1827ceaf690f9a9e5c3

you can get even more power with zsh zle http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html

or simply fc

over 1 year ago ·
15662
0 r4vdwncotdtgf swrrqowqtxk25r7 gwtjttwzlv7dl3knedyo53fvluycla2zy5arb1qtfc9umh

Perhaps you could consider BASH history suggest box https://github.com/dvorka/hstr that greatly simplifies navigation through the history and its management.

over 1 year ago ·
16307
4953930ec47780f7066493fb510a63fb

@glitchmr

Fish has a well deserved spot in the hall of fame as far as shells go, seems valid enough to me ^^

over 1 year ago ·
18012
Dsp4cssd normal

This is much better than Ctrl-R. Thank you very much..

over 1 year ago ·
18220
0a3ae4df418db73a327bc032973be743

I still prefer ZSH, has this built in.

over 1 year ago ·
18239
 david profile hdr

Ctrl+R is broken for me... never worked quite right :( Didn't correctly delete previous prompt when I was scrolling through history and other various glitches.

over 1 year ago ·