bind

Configure or display key bindings in the Bash shell (Readline control).

Category: Shell bash readline keyboard editing

Install

# bind is a Bash builtin command.
# It is available whenever you are using bash.

What it does

bind controls key bindings and Readline behavior in the Bash shell. It allows you to assign key sequences to editing functions, macros, or commands.

How it works (mechanical)

  • Interacts with GNU Readline library.
  • Maps key sequences (e.g., Ctrl+X) to editing functions.
  • Can define keyboard macros.
  • Changes affect the current shell session.
  • Persistent settings are typically stored in ~/.inputrc.

Quick Start

# Show current key bindings
bind -P

10 Practical Examples

# 1) List all key bindings
bind -P
# 2) Show only key sequences
bind -p
# 3) Bind Ctrl+O to run previous command
bind '"\C-o": previous-history'
# 4) Bind a macro (insert text)
bind '"\C-xd": "date\n"'
# 5) Remove a key binding
bind -r "\C-o"
# 6) Bind arrow keys (if misconfigured)
bind '"\e[A": history-search-backward'
# 7) Load bindings from file
bind -f ~/.inputrc
# 8) Edit persistent bindings
nano ~/.inputrc
# 9) Reload .inputrc without restarting shell
bind -f ~/.inputrc
# 10) Show current Readline variables
bind -V

Notes & Gotchas

  • Works only in Bash (not sh or other shells).
  • Changes are session-specific unless saved in ~/.inputrc.
  • Escape sequences must be properly quoted.
  • Useful for fixing broken arrow keys over SSH.
  • Power users can customize editing behavior extensively.

Historical Context

bind leverages the GNU Readline library, which provides Emacs-style command-line editing. It has long been part of Bash to allow advanced customization of interactive shell behavior.

Modern Equivalent

In modern shells like zsh, similar functionality exists via key binding configuration files. However, bind remains the standard way to manage Readline behavior in Bash.

Related Commands

  • stty — configure terminal line settings.
  • reset — restore terminal state.
  • bash — GNU Bourne Again Shell.
  • set — configure shell options.
  • inputrc — Readline configuration file.