The Wayback Machine - https://web.archive.org/web/20250114194404/https://www.geeksforgeeks.org/bind-command-in-linux-with-examples/
Open In App

bind command in Linux with Examples

Last Updated : 01 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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

bind command in Linux – FAQs

How do I reset Bash keybindings to default?

To reset all keybindings to their default state, simply close the terminal and reopen it. Alternatively, you can run:

exec bash

Can I permanently save my custom keybindings?

Yes, you can save your keybindings in your .bashrc file. Add your bind commands to the file, and they will be loaded automatically when you start a new shell session.

How do I check all current keybindings?

Use the following command to list all current keybindings:

bind -p

Is bind only available in Bash?

While bind is a Bash built-in command, other shells may have similar features for configuring keybindings, but the syntax and functionality can vary.


Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

three90RightbarBannerImg