Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

migrated from superuser.com Mar 19 '12 at 16:20

This question came from our site for computer enthusiasts and power users.

2 Answers

up vote 3 down vote accepted

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
share|improve this answer
1  
bind -P | grep 'can be found' is nice, and can be tweaked to show human-readable shortcuts. –  l0b0 Apr 17 '12 at 7:19
add comment

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
    OPTIND=1
    getopts ":auI:E:" a
  else
    var a=E
    OPTIND=1
    OPTARG='self-insert$|digit-argument$|do-lowercase-version$'
  fi
  case "$a" in
    a)
      bind -p |\
        sort |\
        pr -l1 -W"$COLUMNS" -"$(($COLUMNS/30))"
    ;;
    I)
      bind -p |\
        grep -E "$OPTARG" |\
        sort |\
        pr -l1 -W"$COLUMNS" -"$(($COLUMNS/30))"
    ;;
    E)
      bind -p |\
        grep -Ev '(^$)|^#|'"$OPTARG" |\
        sort |\
        pr -l1 -W"$COLUMNS" -"$(($COLUMNS/30))"
    ;;
    u|*)
      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."
      fi
    ;;
  esac
}
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)).

share|improve this answer
    
-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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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