Bash bind builtin command

Updated: 12/29/2017 by Computer Hope

About bind

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.

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

Bind represents keyboard keys using strings of special characters and literals. The special characters are:

\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:
emacs Emacs-style key mapping. This is the default.
emacs-standard The same as emacs.
emacs-meta The same as emacs, with alternate bindings.
emacs-ctlx "Ctrl-X" style emacs binding.
vi Vi-style key mapping.
vi-move The same as vi.
vi-command The same as vi.
vi-insert Similar to vi, but with alternate keymaps for inserting text.
-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 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 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 "\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 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

Important files

/etc/inputrc The default system-wide key bindings configuration file.

alias — Create an alternate name for a command.
set — Set shell attributes, or display environment variables.