Skip to main content

Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.

bind - Bash Readline Key Binding Manager

The bind command is a Bash shell builtin that provides powerful keyboard binding management for the Readline library. It allows users to display current readline key bindings, bind keyboard sequences to readline functions or shell commands, and customize readline behavior for enhanced command-line productivity. The bind command is essential for power users who want to optimize their command-line workflow by creating custom keyboard shortcuts, improving text editing efficiency, and tailoring the shell environment to their specific needs.

Basic Syntax

bind [-m keymap] [-lpsvPSV] [-f filename] [-q function] [-u function] [-r keyseq] [-x keyseq:shell-command] [keyseq:function-name]

Key Map Types

  • emacs - Emacs-style key bindings (default)
  • emacs-standard - Standard Emacs keymap
  • emacs-meta - Meta key combinations
  • emacs-ctlx - Control-X combinations
  • vi - Vi-style key bindings
  • vi-move - Vi movement mode
  • vi-command - Vi command mode
  • vi-insert - Vi insert mode

Common Options

Display Options

  • -l - List all readline function names
  • -p - List functions and bindings in reusable input format
  • -P - List function names and their current bindings
  • -v - List readline variables and values in input format
  • -V - List readline variable names and current values
  • -s - List macro sequences and their values in input format
  • -S - List key sequences that invoke macros and their values

Binding Options

  • -f filename - Read key bindings from specified file
  • -q function - Query which keys invoke the named function
  • -u function - Unbind all keys bound to the named function
  • -r keyseq - Remove binding for specific key sequence
  • -x keyseq:shell-command - Execute shell command when key sequence is entered

Key Map Options

  • -m keymap - Use specified keymap for subsequent bindings

Usage Examples

Basic Display Operations

Viewing Current Bindings

# List all readline functions
bind -l

# Show all current bindings
bind -p

# Show readable bindings with descriptions
bind -P

# Show only macro bindings
bind -S

# Show readline variables
bind -V

Querying Specific Functions

# Find which keys invoke completion
bind -q complete

# Check reverse-search-history binding
bind -q reverse-search-history

# List all functions containing "search"
bind -l | grep search

# Show specific function binding
bind -P | grep "complete"

Basic Key Binding

Simple Function Bindings

# Bind Ctrl+L to clear screen (usually default)
bind '"\C-l": clear-screen'

# Bind Ctrl+R to reverse search history (usually default)
bind '"\C-r": reverse-search-history'

# Bind Ctrl+X Ctrl+U to undo
bind '"\C-x\C-u": undo'

# Bind Alt+F to forward-word
bind '"\ef": forward-word'

# Bind Ctrl+A to beginning-of-line
bind '"\C-a": beginning-of-line'

Custom Text Macros

# Create a macro that types your email
bind '"\C-e": "user@example.com"'

# Bind F1 to show help
bind '"\eOP": "man "'

# Create quick directory navigation
bind '"\C-g": "cd ~/projects\n"'

# Insert current date
bind '"\C-d": "$(date +%Y-%m-%d)"'

Advanced Shell Command Bindings

Executing Shell Commands

# Bind Ctrl+X Ctrl+L to list directory contents
bind -x '"\C-x\C-l": ls -la'

# Bind F2 to show git status
bind -x '"\eOQ": git status'

# Bind Alt+H to show command history
bind -x '"\eh": history | tail -20'

# Bind Ctrl+X Ctrl+T to show system load
bind -x '"\C-x\C-t": uptime'

# Create a custom file explorer
bind -x '"\C-x\C-f": ranger || ls'

Interactive Functions

# Bind F3 to interactive process killer
bind -x '"\eOR": ps aux | fzf | awk "{print \$2}" | xargs kill -9'

# Bind F4 to quick file search
bind -x '"\eOS": find . -name "*.txt" | head -20'

# Bind Ctrl+X Ctrl+D to disk usage analyzer
bind -x '"\C-x\C-d": du -sh * | sort -hr | head -10'

# Create project quick switcher
bind -x '"\C-x\C-p": ls ~/projects/ | fzf | xargs -I {} cd ~/projects/{}'

Key Sequence Notation

Control Keys

  • \C-x - Control+X
  • \C-a - Control+A
  • \c - Control+C

Meta/Alt Keys

  • \M-x or \ex - Meta/Alt+X
  • \ea - Meta/Alt+A

Escape Sequences

  • \e - Escape key
  • \eOP - F1
  • \eOQ - F2
  • \eOR - F3
  • \eOS - F4

Special Characters

  • \" - Quote character
  • \\ - Backslash
  • \C-? - Delete key
  • \e[3~ - Delete (alternative)

Configuration Management

Loading from Files

# Load bindings from ~/.inputrc file
bind -f ~/.inputrc

# Load custom bindings file
bind -f ~/.my_bindings

# Create temporary binding file and load
echo '"\C-x\C-h": "help\n"' > /tmp/temp_bindings
bind -f /tmp/temp_bindings

.inputrc Configuration

# Example ~/.inputrc file content
cat >> ~/.inputrc << 'EOF'

# Custom key bindings
"\C-x\C-e": "emacs\n"
"\C-x\C-v": "vim\n"
"\C-x\C-g": "git status\n"

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

# Show all completions at first tab
set show-all-if-ambiguous on

# Enable vi editing mode
set editing-mode vi

# Custom macros
"\C-x\C-w": "$(whoami)@$(hostname)\n"

EOF

Practical Applications

Development Workflow

Git Integration

# Quick git commit shortcut
bind -x '"\C-x\C-c": git add -A && git commit -m "Quick commit"'

# Git branch switcher
bind -x '"\C-x\C-b": git branch | fzf | xargs git checkout'

# Show recent commits
bind -x '"\C-x\C-l": git log --oneline -10'

# Git push shortcut
bind -x '"\C-x\C-p": git push origin $(git branch --show-current)'

# Git status with diff
bind -x '"\C-x\C-s": git status && git diff --stat"'

Project Management

# Quick project setup
bind -x '"\C-x\C-n": npm install && npm run dev'

# Lint code
bind -x '"\C-x\C-t": npm run lint || flake8 *.py'

# Run tests
bind -x '"\C-x\C-r": npm test || pytest -v'

# Build project
bind -x '"\C-x\C-m": make || npm run build"

# Docker management
bind -x '"\C-x\C-d": docker ps -a"'

System Administration

Process Management

# Interactive process viewer
bind -x '"\C-x\C-p": htop || top'

# Kill process by name selector
bind -x '"\C-x\C-k": ps aux | fzf | awk "{print \$2}" | xargs kill -9'

# Show system information
bind -x '"\C-x\C-i": neofetch || screenfetch'

# Monitor system resources
bind -x '"\C-x\C-m": iostat -x 1 || vmstat 1'

# Network connections viewer
bind -x '"\C-x\C-n": ss -tuln || netstat -tuln"'

File Management

# Quick file search
bind -x '"\C-x\C-f": find . -type f | fzf'

# Recent files viewer
bind -x '"\C-x\C-r": find . -type f -mtime -7 | head -20'

# Disk space analyzer
bind -x '"\C-x\C-d": df -h && du -sh * | sort -hr | head -10'

# Large file finder
bind -x '"\C-x\C-l": find . -type f -size +100M | head -20'

# File permissions viewer
bind -x '"\C-x\C-w": ls -la | head -20"'

Advanced Usage

Vi Mode Configuration

# Switch to vi editing mode
bind -m vi '"\C-p": previous-history'
bind -m vi '"\C-n": next-history'

# Vi movement bindings
bind -m vi-command '"j": history-search-backward'
bind -m vi-command '"k": history-search-forward'

# Vi insert mode bindings
bind -m vi-insert '"\C-a": beginning-of-line'
bind -m vi-insert '"\C-e": end-of-line'

# Custom vi-style bindings
bind -m vi-insert '"\C-w": backward-kill-word'
bind -m vi-insert '"\C-u": unix-line-discard'

Emacs Mode Enhancements

# Enhanced Emacs bindings
bind '"\C-x\C-b": " | fzf\n"'
bind '"\C-x\C-f": "find . -type f | fzf\n"'
bind '"\C-x\C-g": "grep -r \"\" . | fzf\n"'

# Multi-key sequences
bind '"\C-xv": "vim "'
bind '"\C-x\C-c": "clear\n"'

# Word movement enhancements
bind '"\C-f": forward-word'
bind '"\C-b": backward-word'
bind '"\M-f": forward-word'
bind '"\M-b": backward-word'

Smart Completion Bindings

# Context-aware completions
bind -x '"\C-x\C-t": if [[ $PWD == *"git"* ]]; then git status; else ls -la; fi'

# Directory-specific bindings
bind -x '"\C-x\C-l": case $PWD in */projects*) ls -la ;; */logs*) tail -f *.log ;; *) ls ;; esac'

# Smart cd with fzf
bind -x '"\C-c": cd $(find . -type d | fzf) && clear'

# File type-specific actions
bind -x '"\C-x\C-o": case $(file -b --mime-type $(readlink -f f)) in image/*) xdg-open $(readlink -f f) ;; text/*) vim $(readlink -f f) ;; *) file $(readlink -f f) ;; esac'

Custom Functions Integration

Bash Functions with Bind

# Define custom functions for use with bind
my_cd_fzf() {
cd $(find . -type d | fzf)
clear
}

git_status_fzf() {
if [ -d .git ]; then
git status
echo "Changes:"
git diff --name-only | fzf
else
echo "Not in a git repository"
fi
}

quick_search() {
local term=$(read -p "Search term: " term; echo $term)
grep -r "$term" . | head -20
}

# Bind these functions
bind -x '"\C-g": my_cd_fzf'
bind -x '"\C-x\C-s": git_status_fzf'
bind -x '"\C-x\C-q": quick_search'

Environment-Specific Bindings

SSH Session Bindings

# Bindings for SSH sessions
if [ -n "$SSH_CONNECTION" ]; then
bind '"\C-x\C-h": "exit\n"'
bind -x '"\C-x\C-i": who'
bind -x '"\C-x\C-w": w'
bind '"\C-x\C-c": "clear\n"'
fi

Terminal-Specific Bindings

# Terminal-specific configurations
case "$TERM" in
*rxvt*|*xterm*)
bind '"\e[7~": beginning-of-line'
bind '"\e[8~": end-of-line"'
;;
*screen*)
bind '"\e[1~": beginning-of-line'
bind '"\e[4~": end-of-line"'
;;
esac

Performance Optimization

Efficient Bindings

# Use functions instead of complex commands
simple_ls() { ls -la; }
bind -x '"\C-l": simple_ls'

# Avoid heavy operations in frequently used bindings
# Bad: bind -x '"\C-r": find / -name "*.log"' # Too slow!
# Good: bind -x '"\C-r": find . -name "*.log"' # Much faster

# Use shell builtins when possible
bind -x '"\C-c": clear"' # clear is a builtin
# Better than: bind -x '"\C-c": tput clear"'

Memory Considerations

# Remove unused bindings to free memory
bind -r "\C-x\C-unused"

# Use -u to remove all bindings for a function
bind -u some-unused-function

# Clean up temporary bindings after use
bind -r "\C-temp"

Troubleshooting

Common Issues

Key Sequences Not Working

# Debug key sequences
bash -c 'read -p "Press key: " key; printf "%q\n" "$key"'

# Check current bindings
bind -p | grep "C-x"

# Test if readline is working
bind -V | head -5

Conflicting Bindings

# Remove conflicting binding
bind -r "\C-x\C-c"

# Check what function is bound to a key
bind -q some-function

# Show all bindings for a specific keymap
bind -m emacs -p

Terminal Compatibility Issues

# Check terminal type
echo $TERM

# Test terminal capabilities
tput khome # Home key
tput kend # End key

# Use universal escape sequences
bind '"\e[1~": beginning-of-line' # Home
bind '"\e[4~": end-of-line' # End
bind '"\e[3~": delete-char' # Delete

Debugging Bindings

# Show all current bindings in debug format
bind -v

# Test specific key sequence
echo "Testing Ctrl+X..."
read -rsn1 key
if [[ "$key" == $'\x18' ]]; then
echo "Ctrl+X detected"
fi

# Monitor what key sequences are sent
cat -v # Press keys to see their codes
  • readline - Readline library configuration
  • stty - Terminal settings configuration
  • tput - Terminal capability utility
  • export - Environment variable management
  • alias - Command alias creation
  • history - Command history management
  • set - Shell options configuration
  • shopt - Shell options management

Best Practices

  1. Use descriptive key sequences - Choose memorable combinations
  2. Avoid conflicts - Check existing bindings before creating new ones
  3. Document custom bindings - Keep a record of your custom shortcuts
  4. Use separate files - Store complex bindings in dedicated files
  5. Test bindings thoroughly - Ensure they work as expected
  6. Consider portability - Use standard key sequences when possible
  7. Backup configurations - Keep copies of your .inputrc file
  8. Use function names - Prefer function names over raw key sequences
  9. Optimize performance - Use efficient shell commands in bindings
  10. Profile regularly - Review and remove unused bindings

Performance Tips

  1. Use shell builtins instead of external commands when possible
  2. Avoid heavy operations in frequently used bindings
  3. Cache results of expensive operations in shell variables
  4. Use conditional logic to limit binding scope
  5. Minimize subprocess creation in -x bindings
  6. Prefer -x options for complex operations over macros
  7. Keep bindings simple and focused on single tasks
  8. Use appropriate keymaps for different editing modes
  9. Test binding performance with complex commands
  10. Consider terminal latency when designing multi-key sequences

The bind command is an essential tool for power users who want to customize their shell experience. By creating intelligent keyboard shortcuts and integrating custom functions, users can significantly improve their command-line productivity and create a more efficient workflow tailored to their specific needs.