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?


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.

  • 2
    Thanks! I read the documentation, but my eyes passed right over the part about the binding needing to be a single arguments. Enclosing it in quotes fixes the problem. The -m option can be followed by a binding, though; the examples I provided work fine when the binding portion is quoted.
    – Ben
    Jan 25 '17 at 0:29
  • 1
    @Ben is correct. help bind is slightly misleading in the argument list. If you search for the argument order of bind in the full man bash you see that -m is compatible with other args.
    – grochmal
    Jan 25 '17 at 0:38
  • @grochmal, Ben, thanks for the correction on that point; good to know!
    – Wildcard
    Jan 25 '17 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.