Bash bind builtin command
For instance, when you're typing at the bash command line, you can 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 which 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
|\C-||Represents holding down the Ctrl key, 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, the Alt key 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 not sure 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 will display the code for the key you pressed. For instance, if you press Ctrl+v, then F7, you will see:
Here, ^[ is an escape character, so to represent this keycode in a string we can use:
For examples of using this keycode in a command, see examples.
bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function | readline-command]
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.|
bind -m vi
Use vi keymapping in bash, allowing you manipulate text on the command line as you would in vi.
List all bindable editing functions.
Same as the above command, but also lists what the function's current bindings are, if any.
Bind a macro to your F7 key (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 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 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 will look 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 your F4 key 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
|/etc/inputrc||The default system-wide key bindings configuration file.|