144

I found a command a couple of months ago that made my bash history auto-complete on what's already on the line when pressing the up arrow:

$ vim fi

Press

$ vim file.py

I'd like to set this up on my new computer, because it saves a lot of time when keeping a big history. The problem is that I can't for the life of me remember where it was mentioned and reading through endless bash references and tutorials unfortunately didn't help either.

Does anybody know the command?

curusarn
  • 403
  • 4
  • 11
blokkie
  • 5,375
  • 7
  • 24
  • 18

5 Answers5

246

Probably something like

# ~/.inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward

or equivalently,

# ~/.bashrc
if [[ $- == *i* ]]
then
    bind '"\e[A": history-search-backward'
    bind '"\e[B": history-search-forward'
fi

(the if statement checks for interactive mode)

Normally, Up and Down are bound to the Readline functions previous-history and next-history respectively. I prefer to bind PgUp/PgDn to these functions, instead of displacing the normal operation of Up/Down.

# ~/.inputrc
"\e[5~": history-search-backward
"\e[6~": history-search-forward

After you modify ~/.inputrc, restart your shell or use Ctrl+X, Ctrl+R to tell it to re-read ~/.inputrc.


By the way, if you're looking for relevant documentation:

Bash uses The GNU Readline Library for the shell prompt and history.

Uri
  • 25,622
  • 10
  • 45
  • 72
ephemient
  • 198,619
  • 38
  • 280
  • 391
  • Thanks for your answer. This does sound familiar, but unfortunately doesn't work for me. Placing it in .inputrc changes nothing and putting it in .bashrc stop the arrows from doing anything. Any ideas? – blokkie Jun 23 '09 at 01:31
  • Never mind. history-complete-* does not exist; only the history-search-* functions exist. Thanks! – blokkie Jun 23 '09 at 01:32
  • Ah, I clearly mistyped the first two examples there... the last one was correct, though. I'll fix that up. – ephemient Jun 23 '09 at 01:34
  • @user1037114 : this is a question in it self, you should ask it on its own post. – kamaradclimber Nov 19 '11 at 22:23
  • This fixed the up/down arrows for me using vi mode in bash. Note, I have to hit "Esc" (command mode) for it to work. – Chris Feb 08 '15 at 21:14
  • I've added a check for interactive mode to your answer, as I was getting an error message on startup with bind warning. – Uri Apr 17 '15 at 10:46
  • For those using vi mode, `~/.inputrc` works for command mode, and the `bind` version works for insert mode. Using both makes the behavior consistent no matter what mode you're in! – apottere Apr 16 '19 at 18:17
5

Update .inputrc with the following:

"\C-[OA": history-search-backward
"\C-[[A": history-search-backward

"\C-[OB": history-search-forward
"\C-[[B": history-search-forward
nate_weldon
  • 2,289
  • 1
  • 26
  • 32
  • I don't know what these keys are specifically, but this answer fixed this functionality for me when the up and down keys didn't do history search while using tmux and st terminal on X11. – razzintown Mar 22 '16 at 03:54
  • 1
    @razzintown for st you probably have `set enable-keypad on` for the `del` key to work properly ([faq](http://git.suckless.org/st/tree/FAQ)). When keypad is on the arrow keys are `"\C-[OA"` and `"\C-[OB"` (and `"\C-[OC"` and `"\C-[OD"`). I posted an answer bellow to clarify this. – MauricioRobayo Apr 30 '17 at 01:39
  • @Chris Those are keypad mode up and down arrows keys, and ANSI mode up and down arrows keys. See my answer bellow. – MauricioRobayo Apr 30 '17 at 01:46
3

If set enable-keypad on is in your ~/.inputrc as some st (suckless simple terminal) users might, be aware that the arrows keys are in keypad mode. Ubuntu ships with this useful /usr/share/doc/bash/inputrc.arrows:

# This file controls the behaviour of line input editing for
# programs that use the Gnu Readline library.
#
# Arrow keys in keypad mode
#
"\C-[OD"        backward-char
"\C-[OC"        forward-char
"\C-[OA"        previous-history
"\C-[OB"        next-history
#
# Arrow keys in ANSI mode
#
"\C-[[D"        backward-char
"\C-[[C"        forward-char
"\C-[[A"        previous-history
"\C-[[B"        next-history
#
# Arrow keys in 8 bit keypad mode
#
"\C-M-OD"       backward-char
"\C-M-OC"       forward-char
"\C-M-OA"       previous-history
"\C-M-OB"       next-history
#
# Arrow keys in 8 bit ANSI mode
#
"\C-M-[D"       backward-char
"\C-M-[C"       forward-char
"\C-M-[A"       previous-history
"\C-M-[B"       next-history

So I'm not sure if you'll need all, but it might not hurt to have in your ~/.inputrc:

# Arrow keys in keypad mode
"\C-[OA": history-search-backward
"\C-[OB": history-search-forward
"\C-[OC": forward-char
"\C-[OD": backward-char

# Arrow keys in ANSI mode
"\C-[[A": history-search-backward
"\C-[[B": history-search-forward
"\C-[[C": forward-char
"\C-[[D": backward-char

This is also on the same topic: My cursor keys do not work and also this xterm: special keys

MauricioRobayo
  • 2,207
  • 23
  • 26
2

With ohmyzsh, use this in your .zshrc :

bindkey '\e[A' history-search-backward
bindkey '\e[B' history-search-forward

To reload, source ~/.zshrc or relaunch terminal.

Source: https://superuser.com/a/418299/71680

cs01
  • 5,287
  • 1
  • 29
  • 28
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
  • 2
    What worked for me on zsh was the option given in the source link: bindkey '\e[A' history-search-backward bindkey '\e[B' history-search-forward – Michael Massey Nov 25 '14 at 12:17
-3

You may need to enabled bash completion.

Check

  • /etc/profile
  • /etc/bash.bashrc
  • ~/.bashrc

to see if any of the above files source /etc/bash_completion. i.e.

. /etc/bash_completion

If /etc/bash___completion is not sourced by any of the above files you will need to add it to one of them.

If you want all bash users on your machine to have bash completion, source /etc/bash_completion from /etc/bash.bashrc.

If it's just you who wants bash completion, source /etc/bash_completion from your ~/.bashrc.

Convict
  • 506
  • 2
  • 4
  • 2
    Tab completion is nice, but not the same as history recall, which is what the original poster was asking for. – ephemient Jun 23 '09 at 01:53