Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create key binds in the Linux system using the terminal?
To set Readline key bindings and variables in the Linux system, we use the bind command. The bind command is used to assign functions and macros to keys, allowing you to create hotkeys instead of typing entire commands. It is a shell built-in command available in bash.
Syntax
The syntax of the bind command is as follows −
bind [-lpsvPSVX] [-m KEYMAP] [-f FILENAME] [-q NAME] [-u NAME] [-r KEYSEQ] [-x KEYSEQ:shell-command]
While the general syntax appears complex, the bind command is simple to use and allows you to easily create custom key bindings and macros.
Command Options
| Option | Description |
|---|---|
| -l | Display the list of function names |
| -P | Display the list of function names and bindings |
| -p | Display functions and bindings in a form that can be reused as input |
| -S | Display key sequences that invoke macros and their values |
| -V | Display variable names and values |
| -v | Display variable names and values in reusable form |
| -q function-name | Query which keys invoke the named function |
| -r KEYSEQ | Remove the binding for KEYSEQ |
| -f FILENAME | Read key bindings from FILENAME |
| -x | List shell command key bindings |
Examples
Listing Functions and Bindings
To list all the readline function names −
$ bind -l
To list the key bindings and corresponding function names −
$ bind -p
Creating Custom Key Bindings
To create a custom key bind that prints "Hey, welcome to tutorialspoint!" when pressing Ctrl + V −
$ bind '"\C-v":"Hey, Welcome to tutorialspoint!"'
After executing this command, pressing Ctrl + V will insert the text instead of requiring you to type the entire message.
Creating Shell Command Bindings
To bind a shell command to a key combination, use the -x option −
$ bind -x '"\C-l":clear'
This binds Ctrl + L to execute the clear command.
Listing Custom Bindings
To list all custom shell command key bindings −
$ bind -x
Removing Key Bindings
To remove an existing key binding, use the -r option −
$ bind -r "\C-v"
Key Sequence Notation
When creating key bindings, use the following notation for special keys −
| Notation | Key Combination |
|---|---|
| \C- | Control key (e.g., \C-v for Ctrl + V) |
| \M- | Alt/Meta key (e.g., \M-x for Alt + X) |
| \e | Escape key |
Conclusion
The bind command is a powerful tool for creating custom key bindings in Linux bash shell. It allows you to assign text macros or shell commands to key combinations, improving productivity by reducing repetitive typing. Key bindings are session-specific unless saved to configuration files like .bashrc.
