LinuxCommandLibrary

bind

Associate names with network addresses

TLDR

List all bound commands and their hotkeys

$ bind [-p|-P]
copy

Query a command for its hotkey
$ bind -q [command]
copy

Bind a key
$ bind -x '"[key_sequence]":[command]'
copy

List user defined bindings
$ bind -X
copy

Display help
$ help bind
copy

SYNOPSIS

bind [-m keymap] [-lpsvPSVX] | bind [-m keymap] [-q|-u function] [-r keyseq] | bind [-m keymap] -f filename | bind [-m keymap] [-x|keyseq:function-name] | bind [-m keymap] -a keymapname | bind [-m keymap] keyseq:keyseqname

PARAMETERS

-a keymapname
    Add a new keymap with name keymapname

-f filename
    Read key bindings from filename

-l
    List names of all bindable Readline functions

-m keymap
    Set keymap as current for binding operations

-p
    Print readable representation of current key bindings

-P
    List current keymap, bindings, and source

-q function
    Query macro bound to function

-r keyseq
    Remove binding for keyseq

-s
    Print current Readline key sequences

-S
    List keymaps and bindings in -s style

-u function
    Unbind all keys bound to function

-v
    Short form of -p

-V
    List Readline variables and values

-X
    List keymaps, bindings, and source in -p style

keyseq:function-name
    Bind keyseq to Readline function-name

-x keyseq:function-name
    Bind keyseq to shell command function-name

keyseq:keyseqname
    Make keyseq an alias for another keyseqname

DESCRIPTION

The bind command is a Bash built-in utility for managing key bindings in the GNU Readline library, which powers line editing in interactive shells. It allows users to map keystroke sequences to Readline editing functions, shell commands, or other key sequences, enabling personalized input handling.

Common uses include remapping keys for productivity, switching editing modes (Emacs, Vi), or defining macros. For instance, bind Ctrl-A to move to line start: bind '\C-a:beginning-of-line'. Bind a key to a shell command: bind -x '"\C-r": "history | grep ssh"'.

bind supports multiple keymaps (e.g., emacs, vi-insert) via -m, loading bindings from files with -f, listing current bindings (-p, -P), querying/unbinding keys (-q, -u, -r), and more. Output formats aid scripting and debugging.

Primarily used in .bashrc or interactively, it enhances shell usability but requires an interactive Bash session.

CAVEATS

Only functional in interactive Bash shells with Readline enabled. Key sequences use special escapes (e.g., '\C-x' for Ctrl-X). Terminal capabilities may affect binding behavior. Changes are session-specific unless saved to .inputrc.

EXAMPLES

bind -x '"\C-f": "ls"' - List files on Ctrl-F.
bind '\e[A':history-search-backward' - Vi-like history search.
bind -f ~/.inputrc - Load custom bindings.

HISTORY

Introduced in Bash 1.14 (1992) alongside early GNU Readline development by Chet Ramey. Enhanced in Bash 2.0+ with keymap support, shell command binding (-x), and aliasing. Tracks Readline evolution, now at version 8.x.

SEE ALSO

bash(1), readline(3), inputrc(5)

Copied to clipboard