It would be very useful to have a user-friendly way to get an overview of the currently defined keyboard shortcuts. In other words, given the /etc/inputrc lines

"\e[A": history-search-backward
"\e[11~": backward-word

and ~/.inputrc lines

"\e[A": kill-word
"\e\eOD": backward-word

it shouldn't be necessary to go through both files to figure out that \e[A has been overridden, that there are two alternative shortcuts for backward-word, that \e\eOD means ... whatever it means, and that kill-word actually means to delete to the end of the current (or next) word. The ideal solution would print something like

Delete until next word boundary: Meta-A
Go to the previous word boundary: F1; Ctrl-Left
Search history backwards: Meta-Up

(Hopefully someone with a better understanding of escape sequences can fix the shortcuts.)

Is there a Linux program to print this?

I'm guessing such a program might be OS, kernel, or even keyboard-dependent, so maybe it would need extensive configuration (a program which polls the user via showkey or xev to press miscellaneous keys to establish which escape sequences are really sent, for example), but that would be an acceptable price to finally understand how to use terminal shortcuts efficiently.


The short answer is no, I'm pretty sure no such program exists.

You could in principle build one; it would have to look at the readline configuration and at the terminal emulator (the kernel and hardware are not involved).

bind -P | grep 'can be found' in Bash lists the key bindings.

abort can be found on "\C-g", "\C-x\C-g", "\e\C-g".
accept-line can be found on "\C-j", "\C-m".

To have a more descriptive name for the command you'd need to parse the bash or readline documentation.

The correspondance between key sequences and keys is determined by the terminal (usually, the terminal emulator). It is often not readily available, and when it is the method to obtain it is entirely specific to the terminal.

You can come close in Emacs: start emacs -q -nw in a terminal, and press Ctrl+H, C (the describe-key-briefly command) then the key sequence (\e is Escape). This shows you the reconstructed function key, if any, and what the key does in Emacs. Readline's default bindings are strongly inspired by Emacs, so often the function in Emacs is similar to the function in readline, but not always. Example:

Ctrl+H C ESC [ A
<up> runs the command previous-line

I had the very same problem being unable to digest the several pages of output from the bind Bash built-in command, especially when given the -p option.

I therefore came up with the following two functions which I defined in my .bashrc file:

alias def=function
alias val="declare -r"
alias var=declare
alias final="readonly -f"

def keyfunctions {
  if (( "$#" )); then
    var a
    getopts ":auI:E:" a
    var a=E
  case "$a" in
      bind -p |\
        sort |\
        pr -l1 -W"$COLUMNS" -"$(($COLUMNS/30))"
      bind -p |\
        grep -E "$OPTARG" |\
        sort |\
        pr -l1 -W"$COLUMNS" -"$(($COLUMNS/30))"
      bind -p |\
        grep -Ev '(^$)|^#|'"$OPTARG" |\
        sort |\
        pr -l1 -W"$COLUMNS" -"$(($COLUMNS/30))"
      echo >&2 "$FUNCNAME: Usage: $FUNCNAME [-a|-i <eregex>|-e <eregex>]"
      echo >&2 "$FUNCNAME: Invoking $FUNCNAME without arguments assumes -E'self-insert$|digit-argument$|do-lowercase-version$'."
      val -i b="$((OPTIND-1))"
      if [[ "$OPTARG" == : ]]; then
        echo >&2 "$FUNCNAME: Error: Invalid argument \"$OPTARG\" at position $b."
      elif [[ "$OPTARG" == ? ]]; then
        echo >&2 "$FUNCNAME: Error: Invalid option \"$OPTARG\" at position $b."
final keyfunctions

def keymacros {
  bind -s |\
    sort |\
    pr -l1 -W"$COLUMNS" -"$(($COLUMNS/40))"
final keyfunctions

Just execute the above two definitions and decide whether you like the sorted (sort) and tabulated (pr -t) output. The width of columns is fixed the number of columns relates to the ratio of $(($COLUMNS/$width)).

  • -1 This just prints a tabulated version of the bind output, with truncated function names. The "human-readable" part of the question was about transforming for example "\e[A": kill-word into something like Delete until next word boundary: Meta-A (partial implementation). Also, this would be simpler to read if you didn't redefine standard Bash syntax. – l0b0 Nov 17 '12 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.