28

A Useful Thing in Bash

 4 years ago
source link: https://www.tuicool.com/articles/3YjYNvZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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’.

(asmiah 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

November 13, 2012 15:19

1404

tiagox

I'll start to use it.

over 1 year ago ·

November 14, 2012 13:48

1447

sheerun

install zsh ;)

over 1 year ago ·

November 19, 2012 19:18

1646

glitchmr

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

over 1 year ago ·

November 23, 2012 14:50

1785

matleclaire

Or just use Ctrl-R !

over 1 year ago ·

November 23, 2012 17:12

1788

3Iriaab.jpg!web

alyssa

as@matleclaire said, just use ctrl-r!

over 1 year ago ·

November 23, 2012 18:09

1790

uaqEFb7.jpg!web

marinftw

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

over 1 year ago ·

November 23, 2012 18:56

1794

RVJnym6.jpg!web

felipebrnd

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

;)

over 1 year ago ·

December 05, 2012 06:25

2245

QFZFn2N.jpg!web

namuol

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

over 1 year ago ·

December 06, 2012 02:27

2279

6Rr63yM.jpg!web

hoosteeno

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 ·

December 12, 2012 19:17

2431

UzA3qiN.jpg!web

jesusaurus

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 ·

December 14, 2012 13:56

2498

NfmuA32.jpg!web

kjellski

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 ·

December 14, 2012 13:59

2499

NfmuA32.jpg!web

kjellski

@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 ·

December 19, 2012 22:18

2670

RZVzAjq.jpg!web

yeco

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

over 1 year ago ·

December 24, 2012 09:29

2748

2Ize6bi.jpg!web

moiseevigor

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 ·

January 25, 2013 14:46

3451

VNVNNbi.jpg!web

hupili

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 ·

February 17, 2013 23:57

4139

22IvY3B.jpg!web

leech

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 ·

March 09, 2013 21:17

4720

Efyiiu7.jpg!web

santoshkumar

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

over 1 year ago ·

March 26, 2013 09:49

5112

nY7rei3.jpg!web

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 ·

April 17, 2013 18:35

5666

VjqYn2v.jpg!web

jimeh

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 ·

April 17, 2013 18:40

5667

6byu2aF.jpg!web

hauleth

@glitchmr not only you :)

over 1 year ago ·

April 17, 2013 18:44

5668

BBbeyej.png!web

mareizio

zsh is for hipsters :-P

over 1 year ago ·

April 17, 2013 19:16

5671

jAvUZ33.jpg!web

ipetepete

@sheerun or even better: OhMyZsh! https://github.com/robbyrussell/oh-my-zsh.git

over 1 year ago ·

April 17, 2013 19:28

5673

QFJzeqf.jpg!web

dusenyao

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

+

zsh history-substring-search plugin

= awesomeness in ZSH

over 1 year ago ·

April 17, 2013 19:37

5674

sheerun

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

over 1 year ago ·

April 17, 2013 19:46

5675

7nI7Nz2.jpg!web

passionate

awesome tip .

over 1 year ago ·

April 17, 2013 19:51

5676

jEVv2aq.jpg!web

jackcviers

@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 ·

April 17, 2013 20:14

5678

VJBVZjV.png!web

miah

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 ·

April 17, 2013 20:39

5679

u2MZrqV.jpg!web

joaonettome

awesome!

over 1 year ago ·

April 17, 2013 20:40

5680

davidcalvertfr

Or just use :

alias hs="history | grep"

Save it to your .bashrc file to make it permanent.

over 1 year ago ·

April 17, 2013 20:47

5683

RNjEBvb.jpg!web

herson

mind = blow

over 1 year ago ·

April 17, 2013 21:18

5684

BBbeyej.png!web

mareizio

OhMyZsh! is super-hipster!

over 1 year ago ·

April 17, 2013 21:38

5686

uaEvuiI.jpg!web

icodeforlove

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

over 1 year ago ·

April 17, 2013 22:32

5689

RZZf2eA.png!web

daleobrien

Awesome!

over 1 year ago ·

April 17, 2013 23:05

5690

yeIj2if.jpg!web

nfdisco

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 ·

April 17, 2013 23:19

5691

reIFFfb.jpg!web

ghempton

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 ·

April 18, 2013 04:52

5700

UnQZziM.jpg!web

mceachen

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 ·

April 18, 2013 09:37

5709

jMviamV.jpg!web

aniketpant

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

over 1 year ago ·

April 18, 2013 10:03

5711

Efyiiu7.jpg!web

santoshkumar

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 ·

April 18, 2013 13:06

5717

77JVRfI.jpg!web

chparsons

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 ·

April 19, 2013 06:58

5749

2umyQfv.jpg!web

krunk

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 ·

April 20, 2013 12:40

5777

bAJNriA.png!web

gaveen

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 ·

April 21, 2013 16:39

5797

andrewx192

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 ·

May 01, 2013 10:32

6097

utfans05

just get guake....

over 1 year ago ·

May 28, 2013 13:32

6714

nicolas_danelon

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 ·

June 02, 2013 13:08

6823

MZreMjV.jpg!web

aka_the_moonman

no way ... it is awesome

over 1 year ago ·

August 02, 2013 14:01

8321

6F3MzmE.jpg!web

phcostabh

Neat!

over 1 year ago ·

August 07, 2013 02:58

8401

IzQFFrB.jpg!web

jalcine

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 ·

October 21, 2013 06:21

10191

jU3meaF.jpg!web

nikbabu

works on bash

For csh use

bindkey -k up history-search-backward

bindkey -k down history-search-forward

over 1 year ago ·

October 24, 2013 19:23

10301

UNFNvij.jpg!web

webdevwilson

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 ·

November 09, 2013 11:19

10732

none.jpg

08euccs014

Good one

over 1 year ago ·

November 12, 2013 17:29

10806

YfiIBfJ.jpg!web

anentropic

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

on OS X, what's wrong?

over 1 year ago ·

January 02, 2014 21:34

11861

2IJNZ32.jpg!web

nunull

thats totally awesome!

over 1 year ago ·

January 02, 2014 21:34

11862

2IJNZ32.jpg!web

nunull

thats totally awesome!

over 1 year ago ·

February 20, 2014 09:04

12895

z673mi2.jpg!web

draperer

thanks for sharing man

over 1 year ago ·

March 17, 2014 10:16

13515

VrYN7br.jpg!web

upul doluweera

awsome man..

over 1 year ago ·

March 27, 2014 14:29

13736

ZfAj2aV.jpg!web

joseym

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

over 1 year ago ·

June 26, 2014 17:05

15345

muuUZbV.jpg!web

a-b

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 ·

July 15, 2014 04:40

15662

2e6v6rv.jpg!web

dvorka

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 ·

August 23, 2014 13:38

16307

ZzuEn2E.png!web

robjens

@glitchmr

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

over 1 year ago ·

January 09, 2015 22:05

18012

BB7zYvb.jpg!web

nafaabout

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

over 1 year ago ·

February 03, 2015 14:55

18220

IVz22ii.png!web

tallcoder

I still prefer ZSH, has this built in.

over 1 year ago ·

February 04, 2015 18:45

18239

BFjA3mu.jpg!web

davidhq

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 ·

April 06, 2017 22:52

28784

rQbqEbb.jpg!web

xc

:+1:

over 1 year ago ·


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK