Where do I find a list of terminal key codes to remap shortcuts in bash?

38,589

Solution 1

Those are sequences of characters sent by your terminal when you press a given key. Nothing to do with bash or readline per se, but you'll want to know what sequence of characters a given key or key combination sends if you want to configure readline to do something upon a given key press.

When you press the A key, generally terminals send the a (0x61) character. If you press <Ctrl-I> or <Tab>, then generally send the ^I character also known as TAB or \t (0x9). Most of the function and navigation keys generally send a sequence of characters that starts with the ^[ (control-[), also known as ESC or \e (0x1b, 033 octal), but the exact sequence varies from terminal to terminal.

The best way to find out what a key or key combination sends for your terminal, is run sed -n l and to type it followed by Enter on the keyboard. Then you'll see something like:

$ sed -n l
^[[1;5A
\033[1;5A$

The first line is caused by the local terminal echo done by the terminal device (it may not be reliable as terminal device settings would affect it).

The second line is output by sed. The $ is not to be included, it's only to show you where the end of the line is.

Above that means that Ctrl-Up (which I've pressed) send the 6 characters ESC, [, 1, ;, 5 and A (0x1b 0x5b 0x31 0x3b 0x35 0x41)

The terminfo database records a number of sequences for a number of common keys for a number of terminals (based on $TERM value).

For instance:

TERM=rxvt tput kdch1 | sed -n l

Would tell you what escape sequence is send by rxvt upon pressing the Delete key.

You can look up what key corresponds to a given sequence with your current terminal with infocmp (here assuming ncurses infocmp):

$ infocmp -L1 | grep -F '=\E[Z'
    back_tab=\E[Z,
    key_btab=\E[Z,

Key combinations like Ctrl-Up don't have corresponding entries in the terminfo database, so to find out what they send, either read the source or documentation for the corresponding terminal or try it out with the sed -n l method described above.

Solution 2

It is provided via gnu readline library. you should look into man 3 readline to find out its description.

Looks like you also need information about what does escspe codes liks \[A mean. You can find this information in wikipedia ANSI esacape code article.

Solution 3

The easiest way I've found to check what escape sequence is generated by a key or key combo is to press Ctrl+v in a terminal and then press the key/combo you want to know about.

You'll get a sequence like ^[Oa and you'll have to translate the ^[ into \e or \033 or \x1b or whatever other representation your keybinding system expects for the escape character.

It began as an Emacs function named quoted-insert and, since the default keybinds for Bash and Zsh mimic Emacs, they also copied it.

Bash gets it via the GNU Readline input library that other things like Python's import rlcompleter enhancement also use.

Here's the description from the Readline manual:

quoted-insert (C-q or C-v)

Add the next character typed to the line verbatim. This is how to insert key sequences like C-q, for example.

That should work in anything based on GNU Readline.

As for Zsh, which uses its own alternative named ZLE, it only honours the original Emacs C-q binding when in Vi mode, ironically enough, but C-v still works in the default Emacs-like input mode. (Search for quoted-insert in the zshzle or zshall manpages and the second pair of results should list default keybinds.)

For a mnemonic, I advise thinking of Ctrl+v as asking for it verbatim.

Solution 4

Do these codes come from the same source? The last one looks like a GNU readline binding. That's what the user sends to a bash (see rush's answer). The first two, however, look more like terminal control sequences (even though the first one would be an ill-formed one). That's what bash or another program send back to the terminal emulator in order to control cursor movements, text colors, and the like.

Solution 5

Ctrl+V only works for some characters (does not show Tab, Caps, volume keys, Print Screen, Home, End, Pause, Mute) C-v also does not allow for a revers translation or to put it another way, to view existing bindings and what keys they represent.

It does work for most keys characters, but your best bet is to use

dumpkey -1 >> mapreference

to get a full table of your keycode - the signal your specific keyboard sends for each key press. These keycodes are matched by your map file (on Arch: /usr/share/kbd/keymap/*/** folder) but dumpkeys is easier because all your mappings are stored in once place.

Next you'll need the translation tables depending on what format they key is in.

ASCII translation tables

Octal

  000 NUL   020 DLE   040     060 0   100 @   120 P   140 `   160 p 
  001 SOH   021 DC1   041 !   061 1   101 A   121 Q   141 a   161 q 
  002 STX   022 DC2   042 "   062 2   102 B   122 R   142 b   162 r 
  003 ETX   023 DC3   043 #   063 3   103 C   123 S   143 c   163 s 
  004 EOT   024 DC4   044 $   064 4   104 D   124 T   144 d   164 t 
  005 ENQ   025 NAK   045 %   065 5   105 E   125 U   145 e   165 u 
  006 ACK   026 SYN   046 &   066 6   106 F   126 V   146 f   166 v 
  007 BEL   027 ETB   047 '   067 7   107 G   127 W   147 g   167 w 
  010 BS    030 CAN   050 (   070 8   110 H   130 X   150 h   170 x 
  011 HT    031 EM    051 )   071 9   111 I   131 Y   151 i   171 y 
  012 LF    032 SUB   052 *   072 :   112 J   132 Z   152 j   172 z 
  013 VT    033 ESC   053 +   073 ;   113 K   133 [   153 k   173 { 
  014 FF    034 FS    054 ,   074 <   114 L   134 \   154 l   174 | 
  015 CR    035 GS    055 -   075 =   115 M   135 ]   155 m   175 } 
  016 SO    036 RS    056 .   076 >   116 N   136 ^   156 n   176 ~ 
  017 SI    037 US    057 /   077 ?   117 O   137 _   157 o   177 DEL 

Binary

   0000000 NUL    0010000 DLE    0100000      0110000 0    1000000 @    1010000 P    1100000 `    1110000 p
   0000001 SOH    0010001 DC1    0100001 !    0110001 1    1000001 A    1010001 Q    1100001 a    1110001 q
   0000010 STX    0010010 DC2    0100010 "    0110010 2    1000010 B    1010010 R    1100010 b    1110010 r
   0000011 ETX    0010011 DC3    0100011 #    0110011 3    1000011 C    1010011 S    1100011 c    1110011 s
   0000100 EOT    0010100 DC4    0100100 $    0110100 4    1000100 D    1010100 T    1100100 d    1110100 t
   0000101 ENQ    0010101 NAK    0100101 %    0110101 5    1000101 E    1010101 U    1100101 e    1110101 u
   0000110 ACK    0010110 SYN    0100110 &    0110110 6    1000110 F    1010110 V    1100110 f    1110110 v
   0000111 BEL    0010111 ETB    0100111 '    0110111 7    1000111 G    1010111 W    1100111 g    1110111 w
   0001000 BS     0011000 CAN    0101000 (    0111000 8    1001000 H    1011000 X    1101000 h    1111000 x
   0001001 HT     0011001 EM     0101001 )    0111001 9    1001001 I    1011001 Y    1101001 i    1111001 y
   0001010 LF     0011010 SUB    0101010 *    0111010 :    1001010 J    1011010 Z    1101010 j    1111010 z
   0001011 VT     0011011 ESC    0101011 +    0111011 ;    1001011 K    1011011 [    1101011 k    1111011 {
   0001100 FF     0011100 FS     0101100 ,    0111100 <    1001100 L    1011100 \    1101100 l    1111100 |
   0001101 CR     0011101 GS     0101101 -    0111101 =    1001101 M    1011101 ]    1101101 m    1111101 }
   0001110 SO     0011110 RS     0101110 .    0111110 >    1001110 N    1011110 ^    1101110 n    1111110 ~
   0001111 SI     0011111 US     0101111 /    0111111 ?    1001111 O    1011111 _    1101111 o    1111111 DEL

UTF-8

Unicode 
Code Point         Character           UTF-8(hex)         Name

U+0000                                  00               <control>
U+0001                                  01               <control>
U+0002                                  02               <control>
U+0003                                  03               <control>
U+0004                                  04               <control>
U+0005                                  05               <control>
U+0006                                  06               <control>
U+0007                                  07               <control>
U+0008                                  08               <control>
U+0009                                  09               <control>
U+000A                                  0a               <control>
U+000B                                  0b               <control>
U+000C                                  0c               <control>
U+000D                                  0d               <control>
U+000E                                  0e               <control>
U+000F                                  0f               <control>
U+0010                                  10               <control>
U+0011                                  11               <control>
U+0012                                  12               <control>
U+0013                                  13               <control>
U+0014                                  14               <control>
U+0015                                  15               <control>
U+0016                                  16               <control>
U+0017                                  17               <control>
U+0018                                  18               <control>
U+0019                                  19               <control>
U+001A                                  1a               <control>
U+001B                                  1b               <control>
U+001C                                  1c               <control>
U+001D                                  1d               <control>
U+001E                                  1e               <control>
U+001F                                  1f               <control>
U+0020                                  20               SPACE
U+0021                  !               21               EXCLAMATION MARK
U+0022                  "               22               QUOTATION MARK
U+0023                  #               23               NUMBER SIGN
U+0024                  $               24               DOLLAR SIGN
U+0025                  %               25               PERCENT SIGN
U+0026                  &               26               AMPERSAND
U+0027                  '               27               APOSTROPHE
U+0028                  (               28               LEFT PARENTHESIS
U+0029                  )               29               RIGHT PARENTHESIS
U+002A                  *               2a               ASTERISK
U+002B                  +               2b               PLUS SIGN
U+002C                  ,               2c               COMMA
U+002D                  -               2d               HYPHEN-MINUS
U+002E                  .               2e               FULL STOP
U+002F                  /               2f               SOLIDUS
U+0030                  0               30               DIGIT ZERO
U+0031                  1               31               DIGIT ONE
U+0032                  2               32               DIGIT TWO
U+0033                  3               33               DIGIT THREE
U+0034                  4               34               DIGIT FOUR
U+0035                  5               35               DIGIT FIVE
U+0036                  6               36               DIGIT SIX
U+0037                  7               37               DIGIT SEVEN
U+0038                  8               38               DIGIT EIGHT
U+0039                  9               39               DIGIT NINE
U+003A                  :               3a               COLON
U+003B                  ;               3b               SEMICOLON
U+003C                  <               3c               LESS-THAN SIGN
U+003D                  =               3d               EQUALS SIGN
U+003E                  >               3e               GREATER-THAN SIGN
U+003F                  ?               3f               QUESTION MARK
U+0040                  @               40               COMMERCIAL AT
U+0041                  A               41               LATIN CAPITAL LETTER A
U+0042                  B               42               LATIN CAPITAL LETTER B
U+0043                  C               43               LATIN CAPITAL LETTER C
U+0044                  D               44               LATIN CAPITAL LETTER D
U+0045                  E               45               LATIN CAPITAL LETTER E
U+0046                  F               46               LATIN CAPITAL LETTER F
U+0047                  G               47               LATIN CAPITAL LETTER G
U+0048                  H               48               LATIN CAPITAL LETTER H
U+0049                  I               49               LATIN CAPITAL LETTER I
U+004A                  J               4a               LATIN CAPITAL LETTER J
U+004B                  K               4b               LATIN CAPITAL LETTER K
U+004C                  L               4c               LATIN CAPITAL LETTER L
U+004D                  M               4d               LATIN CAPITAL LETTER M
U+004E                  N               4e               LATIN CAPITAL LETTER N
U+004F                  O               4f               LATIN CAPITAL LETTER O
U+0050                  P               50               LATIN CAPITAL LETTER P
U+0051                  Q               51               LATIN CAPITAL LETTER Q
U+0052                  R               52               LATIN CAPITAL LETTER R
U+0053                  S               53               LATIN CAPITAL LETTER S
U+0054                  T               54               LATIN CAPITAL LETTER T
U+0055                  U               55               LATIN CAPITAL LETTER U
U+0056                  V               56               LATIN CAPITAL LETTER V
U+0057                  W               57               LATIN CAPITAL LETTER W
U+0058                  X               58               LATIN CAPITAL LETTER X
U+0059                  Y               59               LATIN CAPITAL LETTER Y
U+005A                  Z               5a               LATIN CAPITAL LETTER Z
U+005B                  [               5b               LEFT SQUARE BRACKET
U+005C                  \               5c               REVERSE SOLIDUS
U+005D                  ]               5d               RIGHT SQUARE BRACKET
U+005E                  ^               5e               CIRCUMFLEX ACCENT
U+005F                  _               5f               LOW LINE
U+0060                  `               60               GRAVE ACCENT
U+0061                  a               61               LATIN SMALL LETTER A
U+0062                  b               62               LATIN SMALL LETTER B
U+0063                  c               63               LATIN SMALL LETTER C
U+0064                  d               64               LATIN SMALL LETTER D
U+0065                  e               65               LATIN SMALL LETTER E
U+0066                  f               66               LATIN SMALL LETTER F
U+0067                  g               67               LATIN SMALL LETTER G
U+0068                  h               68               LATIN SMALL LETTER H
U+0069                  i               69               LATIN SMALL LETTER I
U+006A                  j               6a               LATIN SMALL LETTER J
U+006B                  k               6b               LATIN SMALL LETTER K
U+006C                  l               6c               LATIN SMALL LETTER L
U+006D                  m               6d               LATIN SMALL LETTER M
U+006E                  n               6e               LATIN SMALL LETTER N
U+006F                  o               6f               LATIN SMALL LETTER O
U+0070                  p               70               LATIN SMALL LETTER P
U+0071                  q               71               LATIN SMALL LETTER Q
U+0072                  r               72               LATIN SMALL LETTER R
U+0073                  s               73               LATIN SMALL LETTER S
U+0074                  t               74               LATIN SMALL LETTER T
U+0075                  u               75               LATIN SMALL LETTER U
U+0076                  v               76               LATIN SMALL LETTER V
U+0077                  w               77               LATIN SMALL LETTER W
U+0078                  x               78               LATIN SMALL LETTER X
U+0079                  y               79               LATIN SMALL LETTER Y
U+007A                  z               7a               LATIN SMALL LETTER Z
U+007B                  {               7b               LEFT CURLY BRACKET
U+007C                  |               7c               VERTICAL LINE
U+007D                  }               7d               RIGHT CURLY BRACKET
U+007E                  ~               7e               TILDE
U+007F                                  7f               <control>
U+0080                                  c2 80            <control>
U+0081                                  c2 81            <control>
U+0082                                  c2 82            <control>
U+0083                                  c2 83            <control>
U+0084                                  c2 84            <control>
U+0085                                  c2 85            <control>
U+0086                                  c2 86            <control>
U+0087                                  c2 87            <control>
U+0088                                  c2 88            <control>
U+0089                                  c2 89            <control>
U+008A                                  c2 8a            <control>
U+008B                                  c2 8b            <control>
U+008C                                  c2 8c            <control>
U+008D                                  c2 8d            <control>
U+008E                                  c2 8e            <control>
U+008F                                  c2 8f            <control>
U+0090                                  c2 90            <control>
U+0091                                  c2 91            <control>
U+0092                                  c2 92            <control>
U+0093                                  c2 93            <control>
U+0094                                  c2 94            <control>
U+0095                                  c2 95            <control>
U+0096                                  c2 96            <control>
U+0097                                  c2 97            <control>
U+0098                                  c2 98            <control>
U+0099                                  c2 99            <control>
U+009A                                  c2 9a            <control>
U+009B                                  c2 9b            <control>
U+009C                                  c2 9c            <control>
U+009D                                  c2 9d            <control>
U+009E                                  c2 9e            <control>
U+009F                                  c2 9f            <control>
U+00A0                                  c2 a0            NO-BREAK SPACE
U+00A1                  ¡               c2 a1            INVERTED EXCLAMATION MARK
U+00A2                  ¢               c2 a2            CENT SIGN
U+00A3                  £               c2 a3            POUND SIGN
U+00A4                  ¤               c2 a4            CURRENCY SIGN
U+00A5                  ¥               c2 a5            YEN SIGN
U+00A6                  ¦               c2 a6            BROKEN BAR
U+00A7                  §               c2 a7            SECTION SIGN
U+00A8                  ¨               c2 a8            DIAERESIS
U+00A9                  ©               c2 a9            COPYRIGHT SIGN
U+00AA                  ª               c2 aa            FEMININE ORDINAL INDICATOR
U+00AB                  «               c2 ab            LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
U+00AC                  ¬               c2 ac            NOT SIGN
U+00AD                  ­                c2 ad            SOFT HYPHEN
U+00AE                  ®               c2 ae            REGISTERED SIGN
U+00AF                  ¯               c2 af            MACRON
U+00B0                  °               c2 b0            DEGREE SIGN
U+00B1                  ±               c2 b1            PLUS-MINUS SIGN
U+00B2                  ²               c2 b2            SUPERSCRIPT TWO
U+00B3                  ³               c2 b3            SUPERSCRIPT THREE
U+00B4                  ´               c2 b4            ACUTE ACCENT
U+00B5                  µ               c2 b5            MICRO SIGN
U+00B6                  ¶               c2 b6            PILCROW SIGN
U+00B7                  ·               c2 b7            MIDDLE DOT
U+00B8                  ¸               c2 b8            CEDILLA
U+00B9                  ¹               c2 b9            SUPERSCRIPT ONE
U+00BA                  º               c2 ba            MASCULINE ORDINAL INDICATOR
U+00BB                  »               c2 bb            RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
U+00BC                  ¼               c2 bc            VULGAR FRACTION ONE QUARTER
U+00BD                  ½               c2 bd            VULGAR FRACTION ONE HALF
U+00BE                  ¾               c2 be            VULGAR FRACTION THREE QUARTERS
U+00BF                  ¿               c2 bf            INVERTED QUESTION MARK
U+00C0                  À               c3 80            LATIN CAPITAL LETTER A WITH GRAVE
U+00C1                  Á               c3 81            LATIN CAPITAL LETTER A WITH ACUTE
U+00C2                  Â               c3 82            LATIN CAPITAL LETTER A WITH CIRCUMFLEX
U+00C3                  Ã               c3 83            LATIN CAPITAL LETTER A WITH TILDE
U+00C4                  Ä               c3 84            LATIN CAPITAL LETTER A WITH DIAERESIS
U+00C5                  Å               c3 85            LATIN CAPITAL LETTER A WITH RING ABOVE
U+00C6                  Æ               c3 86            LATIN CAPITAL LETTER AE
U+00C7                  Ç               c3 87            LATIN CAPITAL LETTER C WITH CEDILLA
U+00C8                  È               c3 88            LATIN CAPITAL LETTER E WITH GRAVE
U+00C9                  É               c3 89            LATIN CAPITAL LETTER E WITH ACUTE
U+00CA                  Ê               c3 8a            LATIN CAPITAL LETTER E WITH CIRCUMFLEX
U+00CB                  Ë               c3 8b            LATIN CAPITAL LETTER E WITH DIAERESIS
U+00CC                  Ì               c3 8c            LATIN CAPITAL LETTER I WITH GRAVE
U+00CD                  Í               c3 8d            LATIN CAPITAL LETTER I WITH ACUTE
U+00CE                  Î               c3 8e            LATIN CAPITAL LETTER I WITH CIRCUMFLEX
U+00CF                  Ï               c3 8f            LATIN CAPITAL LETTER I WITH DIAERESIS
U+00D0                  Ð               c3 90            LATIN CAPITAL LETTER ETH
U+00D1                  Ñ               c3 91            LATIN CAPITAL LETTER N WITH TILDE
U+00D2                  Ò               c3 92            LATIN CAPITAL LETTER O WITH GRAVE
U+00D3                  Ó               c3 93            LATIN CAPITAL LETTER O WITH ACUTE
U+00D4                  Ô               c3 94            LATIN CAPITAL LETTER O WITH CIRCUMFLEX
U+00D5                  Õ               c3 95            LATIN CAPITAL LETTER O WITH TILDE
U+00D6                  Ö               c3 96            LATIN CAPITAL LETTER O WITH DIAERESIS
U+00D7                  ×               c3 97            MULTIPLICATION SIGN
U+00D8                  Ø               c3 98            LATIN CAPITAL LETTER O WITH STROKE
U+00D9                  Ù               c3 99            LATIN CAPITAL LETTER U WITH GRAVE
U+00DA                  Ú               c3 9a            LATIN CAPITAL LETTER U WITH ACUTE
U+00DB                  Û               c3 9b            LATIN CAPITAL LETTER U WITH CIRCUMFLEX
U+00DC                  Ü               c3 9c            LATIN CAPITAL LETTER U WITH DIAERESIS
U+00DD                  Ý               c3 9d            LATIN CAPITAL LETTER Y WITH ACUTE
U+00DE                  Þ               c3 9e            LATIN CAPITAL LETTER THORN
U+00DF                  ß               c3 9f            LATIN SMALL LETTER SHARP S
U+00E0                  à               c3 a0            LATIN SMALL LETTER A WITH GRAVE
U+00E1                  á               c3 a1            LATIN SMALL LETTER A WITH ACUTE
U+00E2                  â               c3 a2            LATIN SMALL LETTER A WITH CIRCUMFLEX
U+00E3                  ã               c3 a3            LATIN SMALL LETTER A WITH TILDE
U+00E4                  ä               c3 a4            LATIN SMALL LETTER A WITH DIAERESIS
U+00E5                  å               c3 a5            LATIN SMALL LETTER A WITH RING ABOVE
U+00E6                  æ               c3 a6            LATIN SMALL LETTER AE
U+00E7                  ç               c3 a7            LATIN SMALL LETTER C WITH CEDILLA
U+00E8                  è               c3 a8            LATIN SMALL LETTER E WITH GRAVE
U+00E9                  é               c3 a9            LATIN SMALL LETTER E WITH ACUTE
U+00EA                  ê               c3 aa            LATIN SMALL LETTER E WITH CIRCUMFLEX
U+00EB                  ë               c3 ab            LATIN SMALL LETTER E WITH DIAERESIS
U+00EC                  ì               c3 ac            LATIN SMALL LETTER I WITH GRAVE
U+00ED                  í               c3 ad            LATIN SMALL LETTER I WITH ACUTE
U+00EE                  î               c3 ae            LATIN SMALL LETTER I WITH CIRCUMFLEX
U+00EF                  ï               c3 af            LATIN SMALL LETTER I WITH DIAERESIS
U+00F0                  ð               c3 b0            LATIN SMALL LETTER ETH
U+00F1                  ñ               c3 b1            LATIN SMALL LETTER N WITH TILDE
U+00F2                  ò               c3 b2            LATIN SMALL LETTER O WITH GRAVE
U+00F3                  ó               c3 b3            LATIN SMALL LETTER O WITH ACUTE
U+00F4                  ô               c3 b4            LATIN SMALL LETTER O WITH CIRCUMFLEX
U+00F5                  õ               c3 b5            LATIN SMALL LETTER O WITH TILDE
U+00F6                  ö               c3 b6            LATIN SMALL LETTER O WITH DIAERESIS
U+00F7                  ÷               c3 b7            DIVISION SIGN
U+00F8                  ø               c3 b8            LATIN SMALL LETTER O WITH STROKE
U+00F9                  ù               c3 b9            LATIN SMALL LETTER U WITH GRAVE
U+00FA                  ú               c3 ba            LATIN SMALL LETTER U WITH ACUTE
U+00FB                  û               c3 bb            LATIN SMALL LETTER U WITH CIRCUMFLEX
U+00FC                  ü               c3 bc            LATIN SMALL LETTER U WITH DIAERESIS
U+00FD                  ý               c3 bd            LATIN SMALL LETTER Y WITH ACUTE
U+00FE                  þ               c3 be            LATIN SMALL LETTER THORN
U+00FF                  ÿ               c3 bf            LATIN SMALL LETTER Y WITH DIAERESIS

Decimal / Hex

  Dec Hex    Dec Hex    Dec Hex  Dec Hex  Dec Hex  Dec Hex   Dec Hex   Dec Hex  
  0 00 NUL  16 10 DLE  32 20    48 30 0  64 40 @  80 50 P   96 60 `  112 70 p
  1 01 SOH  17 11 DC1  33 21 !  49 31 1  65 41 A  81 51 Q   97 61 a  113 71 q
  2 02 STX  18 12 DC2  34 22 "  50 32 2  66 42 B  82 52 R   98 62 b  114 72 r
  3 03 ETX  19 13 DC3  35 23 #  51 33 3  67 43 C  83 53 S   99 63 c  115 73 s
  4 04 EOT  20 14 DC4  36 24 $  52 34 4  68 44 D  84 54 T  100 64 d  116 74 t
  5 05 ENQ  21 15 NAK  37 25 %  53 35 5  69 45 E  85 55 U  101 65 e  117 75 u
  6 06 ACK  22 16 SYN  38 26 &  54 36 6  70 46 F  86 56 V  102 66 f  118 76 v
  7 07 BEL  23 17 ETB  39 27 '  55 37 7  71 47 G  87 57 W  103 67 g  119 77 w
  8 08 BS   24 18 CAN  40 28 (  56 38 8  72 48 H  88 58 X  104 68 h  120 78 x
  9 09 HT   25 19 EM   41 29 )  57 39 9  73 49 I  89 59 Y  105 69 i  121 79 y
 10 0A LF   26 1A SUB  42 2A *  58 3A :  74 4A J  90 5A Z  106 6A j  122 7A z
 11 0B VT   27 1B ESC  43 2B +  59 3B ;  75 4B K  91 5B [  107 6B k  123 7B {
 12 0C FF   28 1C FS   44 2C ,  60 3C <  76 4C L  92 5C \  108 6C l  124 7C |
 13 0D CR   29 1D GS   45 2D -  61 3D =  77 4D M  93 5D ]  109 6D m  125 7D }
 14 0E SO   30 1E RS   46 2E .  62 3E >  78 4E N  94 5E ^  110 6E n  126 7E ~
 15 0F SI   31 1F US   47 2F /  63 3F ?  79 4F O  95 5F _  111 6F o  127 7F DEL

As a note, the first 32 characters in each table are reserved control characters. Control characters are not printable characters. They are used to send commands to the PC.

Now if you want something fun but also helpful, this is an escape sequence injection script I'm working on. It uses fzf and ripgrep and fd-find (rust write-up of find) but based on your term. In prints out your term capabilities to the window and then lets you recursively search the term info man page for the command under the cursor (it goes name -> escape sequence in that order).

Select and hit Enter and it prints it to your command line in between quotes and after an echo -e " " but does not execute (just so you can review before I do something silly) .

#!/bin/bash !
fzfcmp () {
    local term
    local FZF_DEFAULT_COMMAND
    FZF_DEFAULT_COMMAND="infocmp $TERM -1"
    term="$(
    infocmp "${_termchoice:-"$TERM"}" -1 | tr '=' '\n' | \
       tr '\t' '' | \
      sed 's/,$//' | fzf \
      --preview='man terminfo | rg -C10 -e {q}' \
        --preview-window='right:80:wrap' --phony \
        --bind="f9:reload:changeterm"
    unset _termchoice
    )"


 if test -n "$(rg -e 'echo' <<<"$READLINE_LINE")"; then
    READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT} $(awk -F= '{print $1}' <<<"${term:-''}" \
    | sed 's/,$//') ${READLINE_LINE:$READLINE_POINT}";
    else
    READLINE_LINE="$(echo 'echo -e' \"${READLINE_LINE:0:$READLINE_POINT} $(awk -F= '{print $1}' <<<"${term:-''}" \
        | sed 's/,$//') ${READLINE_LINE:$READLINE_POINT}\")"
 fi
  READLINE_POINT=$(( ${#term} + READLINE_POINT ))
unset term
}

termfilter () {
    Fzvar="$@"
    local Fzvar
    query="$Fzvar:-"command rg {}"j
fd -uu  . -t f --full-path /usr/share/terminfo -x rg --files-with-matches "\${_query:-"${1:-"/E}"}"

}

fztin () {
if [[ -z "$1" ]]; then
Fvar="."; else
Fvar="$1"
fi
local query
_query "$@"
termfilter  \
  | awk -F/ '{print  $NF}' \
  | sort -u \
  | fzf --bind="alt-p:execute:echo $(infocmp {}) >> $HOME/tmp/{}.tmp" \
  --preview-window "right:85:wrap" \
  --preview="cnat; infocmp {} -l ";
}

changeterm () {
local _termchoice
eval "${_termchoice=+:$(fztin "$@")}" | fzfcmp
}

bind -x '"\et": fzfcmp'
#bind -x '"\em": changeterm  '

The Alt+M function is still in progress but it will print all available terminals then once you select one it will switch to the other command so you can see which terminal has the command line escape sequence you are looking for.

Share:
38,589

Related videos on Youtube

bug
Author by

bug

Updated on September 18, 2022

Comments

  • bug
    bug over 1 year

    For example:

    "\e[1;5C"
    "\e[Z"
    "\e-1\C-i"

    I only know bits and pieces, like \e stands for escape and C- for Ctrl, but what are these numbers (1) and letters (Z)? What are the ;, [ and - signs for?

    Is there only trial and error, or is there a complete list of bash key codes and an explanation of their syntax?

    • Admin
      Admin almost 11 years
      An additional tip for listing the actual readline bindings of the shell is bind -p.
    • Admin
      Admin almost 11 years
      Most such codes are interpreted by your terminal; read about ANSI escape codes on Wikipedia for a large listing.
    • Admin
      Admin almost 11 years
      stty -a displays the terminal's line settings
  • bug
    bug almost 11 years
    I found "\e[1;5C" to be used for ctrl-arrow forward word movement, and "\e[Z" and "\e-1\C-i" for menu-complete-backwards in other tutorials. They have all been used inside the .inputrc file.
  • bug
    bug almost 11 years
    This does not explain my examples.
  • rush
    rush almost 11 years
    Looks like it was small misunderstood. Please look at the answer update.
  • bug
    bug almost 11 years
    There is no information about my specific examples in the article. A page-search for "[A" returned nothing as well.
  • rush
    rush almost 11 years
    @bug please read carefully the article. \[A is in the table "CSI codes" within row: "CSI n A"
  • Uwe
    Uwe almost 11 years
    OK, if they are interpreted by readline then there must be some terminal emulator where these control sequences are bound to some combination of ctrl, alt, and/or shift with function or arrow keys. (I don't know which one.)
  • bug
    bug almost 11 years
    This is a great explanation, thanks! Now it all falls into place, \e-1\C-i is a backwards tab, because control and i inserts a tab and escape followed by -1 says bash to do it backwards once (I goggled this and found some stuff about digit-arguments).
  • cnvzmxcvmcx
    cnvzmxcvmcx almost 9 years
    $ sed -n 1 sed: -e expression #1, char 1: missing command
  • Stéphane Chazelas
    Stéphane Chazelas almost 9 years
    @vib. That's lowercase L not digit 1
  • user367890
    user367890 almost 8 years
    Does tput / infocmp give different results then user input? For example when I do an arrow left I get \x1b [ D when reading input, but both infocmp and tput for kcub1, as in C-name key_left, gives \x1 O D … (TERM=xterm)
  • Stéphane Chazelas
    Stéphane Chazelas almost 8 years
    @user367890, that's down to the keypad mode. You'll probably find that after tput smkx, your terminal sends \e[OD (kcub1) and after tput rmkx, \e[D (cub1, the same code as the sequence that moves the cursor to the left, so that the echo of those keys does move the cursor. Try stty -echoctl; tput rmkx; sleep inf and you'll see the arrow keys do move the cursor when not in keypad mode).
  • user367890
    user367890 almost 8 years
    Very nice! After a quick test. Does this mean I can do tput smkx and after that rely on the information in infocmp? As in; it says kcub1 is \EOD – and hence I can look for \EOD in input and rely on it being key_left? I have a somewhat nasty side project where I try to interpret this in pure bash, for the fun of it. Related: unix.stackexchange.com/a/294935/98945 – I have used curses in python and C earlier. This is for exploring. Guess most people say whhy though ... :P
  • Stéphane Chazelas
    Stéphane Chazelas almost 8 years
    @user367890, from what Thomas (the ncurses and xterm maintainer) says in the answer you link, yes, it should work. keypad mode or application mode refer to the same thing, so (again, according to Thomas) the key specifications in the terminfo database are for when smkx is enabled.
  • jiggunjer
    jiggunjer about 7 years
    I'd add bash --noediting as a handy way to check input codes.
  • Ben Creasy
    Ben Creasy almost 6 years
    I think this used to work for me, but no longer (using GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin16.6.0)), using both GNU sed and darwin's BSD sed - it returns the literal character.