Open In App

bind command in Linux with Examples

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

bind command is a Bash shell builtin command. It is used to set Readline key bindings and variables. The keybindings are the keyboard actions that are bound to a function. So it can be used to change how the bash will react to keys or combinations of keys, being pressed on the keyboard.

Here, we’ll explore how to use the bind command effectively, its syntax, key options, and practical examples to enhance your workflow in Bash.

Syntax

bind [-lpsvPSVX] [-m keymap] [-q name] [-f filename] [-u name] [-r keyseq]
     [-x keyseq:shell-command] [keyseq:readline-function or readline-command]

where,

  • keyseq: This refers to the key sequence or combination of keys that you want to bind to a specific function or command.
  • readline-function: These are functions provided by the Readline library, like moving the cursor, deleting characters, or cutting and pasting text.
  • shell-command: You can bind key sequences to execute shell commands when pressed.

Key Options for the bind Command

OptionDescriptionExample
-m keymapUses the specified keymap for the current command sequence. Acceptable keymaps: emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, vi-command, vi-insert.bind -m vi
-lLists all available Readline function names.bind -l
-PLists all function names along with their bindings.bind -P
-pLists functions and bindings in a reusable format as input.bind -p
-SLists key sequences that invoke macros and their values.bind -S
-sLists key sequences that invoke macros and their values in a reusable format as input.bind -s
-VLists all Readline variable names and their values.bind -V
-vLists Readline variables in a reusable format as input.bind -v
-q function-nameQueries which keys invoke the specified function.bind -q yank
-u function-nameUnbinds all keys bound to the specified function.bind -u yank
-r keyseqRemoves the binding for the specified key sequence.bind -r "\C-y"
-f filenameReads key bindings from the specified file.bind -f bindfile
-x keyseq:shell-commandExecutes the shell command when the specified key sequence is entered.bind -x '"\C-l":ls'
-XLists key sequences bound with -x and associated commands in a reusable format.bind -X

Practical Examples of the bind Command

1. -m:

It use KEYMAP as the keymap for the duration of this command. Here we are using vi keymapping in bash, which allows us to manipulate text on the command line as you would in vi.

bind -m vi
-m

2. -l:

List all the readline function names. There are around 150 functions that are available by default in this list.

bind -l
-l

3. -p:

It will display both the keybindings and the corresponding function names.

bind -p
-p

4. -P:

It will list of all functions along with the bindings where they appear. It is a little bit easier to read when liked to view all the keybindings for a particular function name.

bind -P
-P

5. -f:

It read key bindings from FILENAME. First of all, create a file containing keybindings.

cat > bind

and then write the keybinding in it for example "\C-i": yank. Now to load keybindings from FILENAME.

bind -f bind
bind -p | grep yank 
-f

6. -q:

It is used to view keybindings only for a specific function.

bind -q yank
 -q

7. -r:

Remove all bindings for the particular key sequence.

bind -r "\C-y"
-r

8. -u:

It also unbinds a keybinding. It will remove the key combinations that is assigned to a particular function.

bind -u yank
-u

9. -v:

It is used to view all the readline variables.

bind -v
-v


Note: To check the help page of bind command, use the following command:

bind --help
bind --help

Conclusion

The bind command in Bash is an extremely versatile tool for customizing your command-line experience. By mastering keybindings, you can increase your productivity and make your terminal work the way you want it to.


Next Article

Similar Reads