Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash bind用法: Bind keyboard shortcuts in the Bash shell
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
      1. Display All Key Bindings
      2. Query a Specific Key Binding
      3. Bind a Command to a Key Sequence
      4. Bind a String to a Key Sequence
      5. Load Key Bindings from a File
    5. Tips

[Linux] Bash bind用法: Bind keyboard shortcuts in the Bash shell

Overview

The bind command in Bash is used to set or display keyboard shortcuts and key bindings for the command line interface. It allows users to customize their shell experience by creating shortcuts for frequently used commands or functions.

Usage

The basic syntax of the bind command is as follows:

bind [options] [arguments]

Common Options

  • -P: Display the current key bindings.
  • -q: Query a specific key binding.
  • -x: Bind a command to a key sequence.
  • -f: Read key bindings from a file.
  • -s: Bind a string to a key sequence.

Common Examples

Display All Key Bindings

To display all current key bindings, you can use the following command:

bind -P

Query a Specific Key Binding

To check what command is bound to a specific key sequence, use:

bind -q <key_sequence>

For example, to query the binding for Ctrl+X, you would run:

bind -q "\C-x"

Bind a Command to a Key Sequence

You can bind a specific command to a key sequence. For example, to bind Ctrl+L to clear the terminal, use:

bind -x '"\C-l": clear'

Bind a String to a Key Sequence

To bind a string that can be inserted into the command line, you can use:

bind -s '"\C-h": "Hello, World!"'

Now, pressing Ctrl+H will insert “Hello, World!” into the command line.

Load Key Bindings from a File

If you have a file with predefined key bindings, you can load them using:

bind -f /path/to/bindings_file

Tips

  • Always test your key bindings to ensure they work as expected before relying on them.
  • Use descriptive names for your custom bindings to avoid confusion later.
  • Consider backing up your key bindings in a file for easy restoration or sharing.