0

I'm trying to map some sequences of keystrokes to commands in BASH, leveraging /etc/inputrc for the sequences. In the top answer here:

How can I make bash tab completion behave like vim tab completion and cycle through matching matches?

The user "sth" says that "\e[Z" is the escape sequence for Shift-Tab.

How can I look up these escape sequences? What, for example, is the sequence for Alt+S, or Ctrl+S?

I've also noticed these sequences in BASH echo coloring. Like, for example:

echo -e "a \e[0;31m B \e[0m k"

Will print a (red) B, between a white 'a' and 'k'. What is this arcana? What does "\e[0;31m" mean? (red, obviously, but why not something like "\e[red]")?

Community
  • 1
  • 1
turiyag
  • 2,757
  • 2
  • 22
  • 21

1 Answers1

3

To find what a keystroke produces empirically run cat and then press the key (possibly needing to first hit ctrl-v and then they key).

\e is "escape".

The rest is terminal control sequences.

See man terminfo. See the output from infocmp. Read up about this history of terminals.

Enjoy losing the next 1 to 5 years of your life and getting very annoyed at the world and learning just how wrong everyone on the internet is about this stuff at (virtually) all times.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • 1
    Here's a slightly better empirical tool: `while IFS= read -rsN1 c;do printf '%02X ' '"'"$c"; done`. Here's some Linux/xterm information: http://man7.org/linux/man-pages/man4/console_codes.4.html. Here's some more: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html – rici Jan 21 '15 at 04:09
  • @rici Thanks. That printf gives different output (less directly useful for the escape sequence comparison but still very useful `printf '%q'` would probably be better than `cat` here though). What's the magic with that leading quote in the argument to printf? I know I've seen it before I just can't recall the details. – Etan Reisner Jan 21 '15 at 05:01
  • 1
    printf with a numeric format of an argument starting with a quote or double-quote outputs the character code of the second character. (It's documented: "Arguments to non-string format specifiers are treated as C constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character." – rici Jan 21 '15 at 05:36
  • @rici Ah, yes, thanks. And yes, it is in the documentation if I'd bothered to look at a recent enough copy of the man page instead of the one on the CentOS 5 and 6 machines I had ssh connections to at the time. =) – Etan Reisner Jan 21 '15 at 12:16