Files
the_information_nexus/tech_docs/readline-keybindings.md
2024-05-28 14:01:24 -06:00

3.8 KiB

Complete Guide to Readline Keybindings

What is Readline?

Readline is a library that provides command-line editing and history capabilities. It is used by many command-line applications, including Bash, to facilitate user input.

Common Readline Keybindings

Cursor Movement
  • ctrl-a: Move to the beginning of the line.
  • ctrl-e: Move to the end of the line.
  • ctrl-f: Move forward one character.
  • ctrl-b: Move backward one character.
  • alt-f: Move forward one word.
  • alt-b: Move backward one word.
Editing
  • ctrl-k: Delete from the cursor to the end of the line.
  • ctrl-u: Delete from the cursor to the beginning of the line.
  • ctrl-w: Delete the word before the cursor.
  • alt-d: Delete the word after the cursor.
  • ctrl-d: Delete the character under the cursor.
  • ctrl-h: Delete the character before the cursor (backspace).
Text Insertion and Replacement
  • ctrl-y: Yank (paste) the last deleted text.
  • alt-r: Undo all changes to the current line.
  • ctrl-t: Transpose the character before the cursor with the character under the cursor.
Case Conversion
  • alt-u: Convert the word after the cursor to uppercase.
  • alt-l: Convert the word after the cursor to lowercase.
  • alt-c: Capitalize the word after the cursor.
Miscellaneous
  • ctrl-l: Clear the screen and redraw the current line at the top.
  • ctrl-_ or ctrl-x ctrl-u: Undo the last editing command.
  • ctrl-r: Reverse search through command history.
  • ctrl-s: Forward search through command history.
  • ctrl-p: Previous command in history.
  • ctrl-n: Next command in history.
Command History
  • ctrl-p: Previous command in history.
  • ctrl-n: Next command in history.
  • alt-.: Use the last argument of the previous command.
Process Management
  • ctrl-c: Interrupt/Kill the current process.
  • ctrl-z: Suspend the current process.
  • ctrl-d: Logout from the current session or close the terminal (EOF).

Viewing and Customizing Readline Keybindings

On Linux

  1. View Current Keybindings:
    bind -P
    
  2. Customizing Keybindings:
    • Edit the ~/.inputrc file. If it does not exist, create it.
    • Add custom keybindings. For example:
      "\C-a": beginning-of-line
      
    • Apply changes by restarting the terminal or running:
      bind -f ~/.inputrc
      

On macOS

  1. View Current Keybindings:
    bind -P
    
  2. Customizing Keybindings:
    • Edit or create the ~/.inputrc file.
    • Example customization:
      "\C-a": beginning-of-line
      

Common .inputrc Customizations

Here are useful customizations you might add to your ~/.inputrc:

# Enable vi mode
set editing-mode vi

# Enable case-insensitive completion
set completion-ignore-case on

# Show all possibilities for completion at once
set show-all-if-ambiguous on

# Do not add space after autocompletion
set completion-suppress-space on

# Custom keybindings
"\C-a": beginning-of-line
"\C-e": end-of-line
"\C-k": kill-line
"\C-u": unix-line-discard
"\C-w": unix-word-rubout
"\M-d": kill-word

Applying Changes

After editing your ~/.inputrc, apply the changes by restarting your terminal or running:

bind -f ~/.inputrc

Example Workflow Using Keybindings

  1. Move to the beginning of the line:
    ctrl-a
    
  2. Delete the word before the cursor:
    ctrl-w
    
  3. Type a new command (e.g., cat):
    cat src/pages/index.js
    

This guide provides an overview of common Readline keybindings, how to view and customize them on Linux and macOS, and how to apply these customizations for a more efficient command-line experience.