Last Updated : 01 Oct, 2024
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.
Syntaxbind [-lpsvPSVX] [-m keymap] [-q name] [-f filename] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
where,
-m keymap
Uses 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
-l
Lists all available Readline function names. bind -l
-P
Lists all function names along with their bindings. bind -P
-p
Lists functions and bindings in a reusable format as input. bind -p
-S
Lists key sequences that invoke macros and their values. bind -S
-s
Lists key sequences that invoke macros and their values in a reusable format as input. bind -s
-V
Lists all Readline variable names and their values. bind -V
-v
Lists Readline variables in a reusable format as input. bind -v
-q function-name
Queries which keys invoke the specified function. bind -q yank
-u function-name
Unbinds all keys bound to the specified function. bind -u yank
-r keyseq
Removes the binding for the specified key sequence. bind -r "\C-y"
-f filename
Reads key bindings from the specified file. bind -f bindfile
-x keyseq:shell-command
Executes the shell command when the specified key sequence is entered. bind -x '"\C-l":ls'
-X
Lists 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 vi2. -l:
List all the readline function names. There are around 150 functions that are available by default in this list.
bind -l3. -p:
It will display both the keybindings and the corresponding function names.
bind -p4. -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 -P5. -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 yank6. -q:
It is used to view keybindings only for a specific function.
bind -q yank7. -r:
Remove all bindings for the particular key sequence.
bind -r "\C-y"8. -u:
It also unbinds a keybinding. It will remove the key combinations that is assigned to a particular function.
bind -u yank9. -v:
It is used to view all the readline variables.
bind -v
Note: To check the help page of bind command, use the following command:
bind --helpConclusion
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4