bind command in Linux with Examples
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.
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
Option | Description | Example |
---|
-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 vi

2. -l:
List all the readline function names. There are around 150 functions that are available by default in this list.
bind -l

3. -p:
It will display both the keybindings and the corresponding function names.
bind -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

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

6. -q:
It is used to view keybindings only for a specific function.
bind -q yank

7. -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 yank

9. -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 --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.
Similar Reads
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version
1 min read
atq command in linux with examples
atq displays the list of pending jobs which are scheduled by the user. If the user is the superuser then the pending jobs of all the users will be displayed. The output lines display Job number, date, hour, queue, and username for each job. Syntax: atq [-V] [-q queue] Options: -V: It will display th
2 min read
autoconf command in Linux with examples
autoconf command is used in Linux to generate configuration scripts. Generate a configuration script from a TEMPLATE-FILE if given, or from âconfigure.acâ if present, or 'configure.in'. The output is sent to the standard output if TEMPLATE-FILE is given, otherwise, it is sent into âconfigureâ. To us
1 min read
autoheader command in Linux with Examples
autoheader command in Linux is used to create a template file of C â#defineâ or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This comma
3 min read
automake command in Linux with Examples
automake is a tool used for automatically generating Makefile.in files compliant with the set GNU Coding Standards. autoconf is required for the use of automake. automake manual can either be read on-line or downloaded in the PDF format. More formats are also offered for download or on-line reading.
1 min read
autoreconf command in Linux with examples
autoreconf is a Autotool which is used to create automatically buildable source code for Unix-like systems. Autotool is a common name for autoconf, automake, etc. These all together are termed as Autotools. Important Points: Provides Portability of source code packages by automatic buildable capabil
2 min read
AWK command in Unix/Linux with examples
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
banner command in Linux with examples
The 'banner' command in Linux is a simple yet powerful utility used to display text in large ASCII characters on the terminal. This command can be particularly useful for creating prominent messages or headings within scripts and outputs.Syntax of the 'banner' Commandbanner textbanner 'Command' Exam
3 min read
basename Command in Linux with examples
The 'basename' command in Linux is a fundamental utility used in file manipulation and script writing. It simplifies file paths by stripping directory information and optional suffixes from file names. Here a detailed overview of the 'basename' command, including its syntax, options, and practical u
3 min read
batch command in Linux with Examples
batch command is used to read commands from standard input or a specified file and execute them when system load levels permit i.e. when the load average drops below 1.5. Syntax: batch It is important to note that batch does not accepts any parameters. Other Similar Commands: atq: Used to display th
1 min read