8 Linux Bash Shell Readline Bind Command Examples

In bash shell, using bind command you can view and modify the readline keybindings.

You can also set a value to a readline variable, or macro, or function.

You can either put this in the .inputrc, you can add it using the bind command rom the command line.

In the command line, by default it uses the emacs keybinding, but you can change it to vi keybindings.

1. List all Readline Functions

Using -l option, you can view all the readline function names. There are around 150 functions that are available by default.

# bind -l
abort
accept-line
alias-expand-line
arrow-key-prefix
backward-byte
backward-char
backward-delete-char
backward-kill-line
backward-kill-word
backward-word
beginning-of-history
beginning-of-line
...

# bind -l | wc -l
154

2. Display Keybindings and Function Names

Use the -p option, which will display both the keybindings and the corresponding function names. This is very easy to read as shown below.

# bind -p
"\C-g": abort
"\C-x\C-g": abort
"\e\C-g": abort
"\C-j": accept-line
"\C-m": accept-line
"\C-b": backward-char
"\eOD": backward-char
"\e[D": backward-char
"\C-h": backward-delete-char
"\C-?": backward-delete-char
"\C-x\C-?": backward-kill-line
"\e\C-h": backward-kill-word
"\e\C-?": backward-kill-word

3. Display List based on Functions

You can also view list of all functions along with the bindingins where they appear. This is little bit easier to read, when you like to view all the keybindings for a particular function name.

When there are multiple keybindings for the name function, it displays only one line item in the output for that particular function as shown below.

# bind -P
abort can be found on "\C-g", "\C-x\C-g", "\e\C-g".
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys
arrow-key-prefix is not bound to any keys
backward-byte is not bound to any keys
backward-char can be found on "\C-b", "\eOD", "\e[D".
backward-delete-char can be found on "\C-h", "\C-?".
backward-kill-line can be found on "\C-x\C-?".
backward-kill-word can be found on "\e\C-h", "\e\C-?".
backward-word can be found on "\eOd", "\e[1;5D", "\e[5D", "\eb".
beginning-of-history can be found on "\e<".
beginning-of-line can be found on "\C-a", "\eOH", "\e[1~", "\e[H".
call-last-kbd-macro can be found on "\C-xe".
capitalize-word can be found on "\ec".
...

4. Read Keybindings from a File

For some reason, if you don't like to use the default ~/.intputrc file, you can also define your own custom keybindings in a different file.

For example, the following mybindings file has a custom keybindings.

# cat mybindings
"\C-i": yank

The following command will load the keybindings from the mybindings file

# bind -f mybindings
 
# bind -p | grep yank
"\C-i": yank

5. Query based on Function

If you want to view keybindings only for a specific function, use the query option -q as shown below.

The following displays keybindings only for the function yank.

# bind -q yank
yank can be invoked via "\C-i", "\C-y".

6. Remove a Keybinding based on Name

You can also unbind a keybinding using -u option. This will remove the key combinations that is assigned to a particular function.

The following will unbind the keys bound to yank function.

# bind -u yank

# bind -p | grep yank
# yank (not bound)

7. Remove a Keybinding based on Keys

Similar to the previous example, you can also remove keybindings based on key combinations (instead of the name).

The following will remove the keybinding that is assigned to Ctrl-i (which in this example is yank function)

# bind -r "\C-i"

# bind -p | grep yank
# yank (not bound)

8. Display Readline Variables

To view all the readline variables using -v option. You can use either -v or -V.

# bind -v
set bind-tty-special-chars on
set blink-matching-paren on
set byte-oriented off
set completion-ignore-case off
set convert-meta off
set disable-completion off
set echo-control-characters on
set enable-keypad off
set enable-meta-key on
set expand-tilde off
set history-preserve-point off
set horizontal-scroll-mode off
...

Comments on this entry are closed.