Bash bind builtin command
On Unix-like operating systems, bind is a builtin command of the Bash shell. You can use it to change how bash responds to keys, and combinations of keys, being pressed on the keyboard.
Description
When you're typing at the bash command line, press Ctrl+A to move the cursor to the beginning of the line, or Ctrl+E to move it to the end. These are "key bindings" — your keyboard actions are bound to a function that moves the cursor.
In bash, the default key bindings correspond to the ones in the Emacs editor, but you can change them to what works best for you.
How bind represents keys
Bind represents keyboard keys using strings of special characters and literals. The special characters are:
\C- | Represents holding down Ctrl, and pressing the keys which follow the dash. |
\e | The Escape key. This is used for other bindings, such as the Meta key. For example, Alt is a meta key on many keyboards. |
It can be very confusing, and the way that bash encodes keys is not always clearly documented. To see how bind encodes default key combinations, you can run bind -P.
You can also look at the binding configurations listed in the file /etc/inputrc. For unusual combinations, you can use Ctrl+V to find any keycode, as described below.
Finding a keycode
If you're unsure what the code is for a particular key combination, you can press Ctrl+V at a bash prompt, then press the key combo. This action is called quoted-insert, and it displays the code for the key you pressed. For instance, if you press Ctrl+V, then F7, you will see:
^[[18~
Here, ^[ is an escape character, so to represent this keycode in a string we can use:
"\e[18~"
For examples of using this keycode in a command, see examples.
Syntax
bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function | readline-command]
Options
The bind builtin command has the following options:
-m keymap | Use keymap as the key mapping scheme for the duration of the current command sequence. The possible values of keymap are:
|
||||||||||||||||
-l | List the names of bindable editing functions. | ||||||||||||||||
-P | List the names of bindable editing functions, and what their bindings are, if any. | ||||||||||||||||
-p | Same as -P, but provides output in a form that can be used as input to the bind command. | ||||||||||||||||
-S | List key sequences that invoke macros, and their values. | ||||||||||||||||
-s | Same as -S, but provides output in a form that can be used as input to bind. | ||||||||||||||||
-V | List variables, and their values, which are used in bash key binding. | ||||||||||||||||
-v | Same as -V, but provides output in a form that can be used as input to bind. | ||||||||||||||||
-q function-name | Display (query) the key binding for the bash function function-name. | ||||||||||||||||
-u function-name | Unbind all keys bound to the editing function function-name. | ||||||||||||||||
-r keyseq | Remove all bindings for the key sequence keyseq. | ||||||||||||||||
-f filename | Read key bindings from the file filename, and use them as input for the bind command. | ||||||||||||||||
-x keyseq:shell-command | Bind a command. The shell command shell-command will be executed by bash when it receives the keyboard sequence keyseq. | ||||||||||||||||
-X | List the key sequences bound by -x in a form suitable for input to bind. |
Examples
bind -m vi
Use vi keymapping in bash, allowing you manipulate text on the command line as you would in vi.
bind -l
List all bindable editing functions.
bind -P
Same as the above command, but also lists what the function's current bindings are, if any.
bind '"\e[18~":"Hi!"'
Bind a macro to F7 (keycode \e [ 1 8 ~) with the text Hi!. When you press F7, Hi! will be inserted at the command line as if you had typed it. Note that both sides of the colon are individually enclosed in double quotes, and the entire argument is enclosed in single quotes. For more information about keycodes, see finding a keycode, above.
bind "\C-k":kill-line
Bind Ctrl+K to the kill-line function, which in bash means to cut all text from the cursor to the end of the line.
bind "\C-y":yank
Bind Ctrl+Y to the yank function, which in bash means to paste the last text that was cut.
bind -q yank
Report what key combination is bound to yank function. Output looks like this:
yank can be invoked via "\C-y".
bind -r "\C-y"
Remove all bindings for the key sequence \C-y. The yank function is no longer bound to anything.
bind -x '"\eOS":"fortune | cowsay"'
Bind F4 to run the command fortune | cowsay.
bind -p > mybinds
Output all key bindings to a file called mybinds, in a format that can be used as input to bind. For example, you can open mybinds in a text editor and make changes to bindings, and save the file. Then, you can apply your changes with the next command.
bind -f mybinds
Configuration file
/etc/inputrc | The default system-wide key bindings configuration file. |
Related commands
alias — Create an alternate name for a command.
set — Set shell attributes, or display environment variables.