Where developers come to connect, share, build and be inspired.

952

The single most useful thing in bash

129958 views

Create ~/.inputrc and fill it with this:

"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on

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 /".

Comments

  • Photo_on_08.01.2013_at_04.15
    sheerun

    install zsh ;)

  • Ava_normal
    matleclaire

    Or just use Ctrl-R !

  • Blank-mugshot
    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.

  • Blank-mugshot
    glitchmr

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

  • Blank-mugshot
    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) 
    

  • Blank-mugshot
    namuol

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

  • Yo
    marinftw

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

  • Blank-mugshot
    yeco

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

  • Blank-mugshot
    santoshkumar

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

  • 9ec1622080b88da39e3369a72a5a2047_normal
    davidcalvertfr

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

  • Blank-mugshot
    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

  • Alyssa
    alyssa

    as @matleclaire said, just use ctrl-r!

  • Blank-mugshot
    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.

  • Profie_4_3
    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.

  • Yo_nueva
    leech

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

  • Blank-mugshot
    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.

  • Blank-mugshot
    felipebrnd

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

    ;)

  • Blank-mugshot
    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.

  • Blank-mugshot
    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

  • Avatar-new-500
    tiagox

    I'll start to use it.

  • Profie_4_3
    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.

  • Blank-mugshot
    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!

  • Blank-mugshot
    jasdeepkhalsa

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

  • Laughingman
    hauleth

    @glitchmr not only you :)

  • Default_profile_2_normal
    mareizio

    zsh is for hipsters :-P

  • Blank-mugshot
    ipetepete
  • Blank-mugshot
    dusenyao

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

    +

    zsh history-substring-search plugin

    = awesomeness in ZSH

  • Photo_on_08.01.2013_at_04.15
    sheerun

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

  • Blank-mugshot
    passionate

    awesome tip .

  • Blank-mugshot
    jackcviers

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

  • Blank-mugshot
    joaonettome

    awesome!

  • 54e2ad6fd33097654a99a61818f61e0a
    herson

    mind = blow

  • Default_profile_2_normal
    mareizio

    OhMyZsh! is super-hipster!

  • 71a3e2556ab7c41a1331422282efddf4
    icodeforlove

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

  • Blank-mugshot
    daleobrien

    Awesome!

  • Blank-mugshot
    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.

  • Blank-mugshot
    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.

  • Bnw
    aniketpant

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

  • Blank-mugshot
    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.

  • Blank-mugshot
    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

  • Blank-mugshot
    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.

  • Juiced602_normal
    juiced

    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

  • Blank-mugshot
    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.

  • 51190104c0b12f6d8a507b93708d2abc_normal
    andrewx192

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

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

  • 0_tlypqpaxzibkuidqfhguqx0y4arnb8dqibe2qxj8fdxkexondimxh0wm9qkwqbh486ysw47hwsmo
    utfans05

    just get guake....

  • Nicolas-danelon-web-developer-php-html-css_reasonably_small_normal
    nicolas_danelon

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

  • Blank-mugshot
    lansizhong

    no way ... it is awesome

  • Blank-mugshot
    phcostabh

    Neat!

  • 0_uihhf6vabsacjihhu6icf54_fpomuirhmgpbf5iy4soyk7fecl4xtlxtdael42axsho9hgzhkotl
    nikbabu

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

  • Blank-mugshot
    webdevwilson

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

  • Blank-mugshot
    rbsl-mohit

    Good one

  • E8_normal
    anentropic

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

    on OS X, what's wrong?

  • Blank-mugshot
    nunull

    thats totally awesome!

  • Blank-mugshot
    nunull

    thats totally awesome!

  • My-face
    draperer

    thanks for sharing man

  • 0_zsvrnl4h8cuhwoey9ofhnbvhutd7woryvutknbwvpkjxns2oqw5c4fjucfskojvtmmblmhbjmeom
    upul doluweera

    awsome man..

  • Morton_066e
    joseym

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

Add a comment