bind
Associate names with network addresses
TLDR
List all bound commands and their hotkeys
Query a command for its hotkey
Bind a key
List user defined bindings
Display help
SYNOPSIS
bind [options] [keyseq:function | keyseq:"macro"]
bind -f filename
bind -r keyseq
bind -u function
bind -x keyseq:command
bind -l|-P|-p|-s|-v|-V [-m keymap]
PARAMETERS
-m keymap
Specifies the keymap to use. Common keymaps include emacs, vi-insert, and vi-command. When used with display options (`-P`, `-p`), it lists bindings for that specific keymap.
-l
Lists the names of all readline functions.
-P
Lists current readline function names and the keys they are bound to. Output format shows function first, then its bindings.
-p
Lists current readline key bindings in a format that can be re-read as input to `bind`. This is useful for saving and restoring bindings.
-r keyseq
Removes (unbinds) the binding for keyseq.
-s
Displays readline key sequences bound to macros and the strings they output. Macros are sequences of characters that are inserted when a key is pressed.
-u function
Unbinds all keys that are bound to the specified function.
-v
Lists current readline variable names and their values. These variables control readline behavior (e.g., `editing-mode`).
-V
Displays the current readline version information.
-f filename
Reads key bindings from filename. This allows for defining multiple bindings in a script or configuration file.
-x keyseq:command
When keyseq is typed, command is executed in the shell. This provides advanced customization by allowing direct shell command execution on a key press.
keyseq:function
Binds keyseq to function. Keyseq specifies the keyboard sequence (e.g., `"\C-x\C-r"` for Ctrl+X Ctrl+R). Function is a readline function name (e.g., `re-read-init-file`).
keyseq:"macro"
Binds keyseq to a macro, which is a literal string that gets inserted when the key sequence is pressed. The string must be enclosed in quotes (e.g., `bind '"\C-o": "Hello, World!\n"'`).
DESCRIPTION
The `bind` command in Linux is a bash shell builtin used to customize and display the key bindings for the readline library. Readline is a fundamental component of interactive command-line interfaces, most notably bash, providing features like command history, line editing, and tab completion.
Users leverage `bind` to tailor their terminal experience by assigning specific keyboard sequences to readline functions. For instance, you can bind `Ctrl+K` to delete text from the cursor to the end of the line, or remap existing shortcuts. This allows for personalized workflows and increased efficiency.
Beyond setting new bindings, `bind` can also list current bindings, show available readline functions, display variable settings, or read bindings from a file. Changes made with `bind` are effective immediately but are temporary, applying only to the current shell session unless saved in a startup file like `~/.bashrc` or `~/.inputrc`.
CAVEATS
`bind` is a bash shell builtin, meaning its availability and behavior are specific to bash (and shells that implement similar readline integration). While readline is a library, `bind` itself is typically part of the shell's builtins.
Changes made with `bind` are only active for the current shell session. For persistent bindings, they must be added to `~/.bashrc` (for bash startup) or `~/.inputrc` (for readline applications in general). Be cautious when modifying default key bindings, as it can disrupt familiar command-line editing workflows. Incorrect bindings can render certain shell functions unusable.
<I>READLINE</I> MODES AND `~/.INPUTRC`
The `bind` command respects the current readline editing mode, typically Emacs mode (default) or Vi mode. You can switch between these modes using `set -o emacs` or `set -o vi`. Bindings created or listed via `bind` will apply to the active mode or can be specified for a particular mode using `-m keymap`.
For permanent key binding changes that apply to all readline-enabled applications (not just bash), bindings should be placed in the `~/.inputrc` file. This file is read by the readline library upon application startup. The `bind` command allows testing and temporary modification, while `~/.inputrc` is for persistent configuration.
KEY SEQUENCE REPRESENTATION
When specifying keyseq for `bind`, special characters are often represented using backslash escapes. Common examples include:
- `\C-x`: Represents Ctrl+X. (`\C` for Control)
- `\M-x`: Represents Alt+X or Meta+X. (`\M` for Meta/Alt)
- `\e`: Represents the Escape key.
- `\n`: Represents a newline character.
HISTORY
The `bind` command's functionality is deeply intertwined with the development of the readline library, which was created by Brian Fox for the GNU Project. Readline became an integral part of the GNU Bash shell since its early versions, providing powerful command-line editing capabilities that were lacking in many traditional Unix shells.
The `bind` builtin was introduced to allow users to customize these readline behaviors directly from the shell prompt or through configuration files like `~/.inputrc` and `~/.bashrc`. Its core purpose and syntax have remained largely consistent over decades, evolving primarily with new readline functions and options. It represents a cornerstone of bash's user experience customization.