Bash – How to rebind next-history and previous-history in Bash

bash

I've set Bash to use vi keybindings with set -o vi, but I'd like to keep the Emacs-style Ctrl-p and Ctrl-n bindings for cycling through history. I've read the "bind" section in bash(1) and tried variations of the following:

bind -m vi-insert "\C-p": previous-history
bind -m vi-insert "\C-n": next-history

It seems that none of the bind commands I issue have any effect, though. The output of bind -P is always unchanged afterwards. How can I change the keybindings for these commands?

Best Answer

From help bind:

The non-option argument syntax is equivalent to
that found in ~/.inputrc, but must be passed as a single argument:
e.g., bind '"\C-x\C-r": re-read-init-file'.

I used set -o emacs and bind -p | grep history to find the arguments necessary.

The commands you need are:

bind '"\C-p": previous-history'
bind '"\C-n": next-history'

I tested this after set -o vi to verify they are the correct commands.


Also note that help is what you use to learn about shell builtins. help itself is a shell builtin, as is type and bind.

Related Question