0

I wanted to find out what my terminal is sending for Ctrl+Backspace and Alt+Backspace, the standard way to do this is to run cat on the terminal and typing stuff usually works, but with certain output like these, the results are tricky.

I am guessing that Alt+Backspace is sending \x1b\x7f (that is, escape backspace) but what's happening if I run cat and type Ctrl+V and Alt+Backspace, or just Alt+Backspace, what happens is that the escape will get "typed" and then immediately it is removed with the backspace so it looks like nothing is happening. I only got clued into this once by seeing my computer render a single frame of the ^[ escape being there.

So far I am not sure how to work out what Ctrl+Backspace is sending. it's not Ctrl+W even though both delete a word on the bash prompt, because under cat it is doing nothing while Ctrl+W deletes a word!

3

3 Answers 3

3

In zsh, with:

$ STTY='raw -echo min 0 time 40' cat -vte
^[^?

Or with bash:

$ s=$(stty -g); stty raw -echo min 0 time 40; cat -vte; stty "$s"
^[^?

You'll see the terminal input raw as it sends it and as rendered by cat -vte. Enter nothing for 4 seconds (40 deciseconds) to stop.

You can replace cat -vte with od -An -w1 -tu1 -tx1 -to1 -ta (assuming GNU od or compatible) for instance to see each byte value in decimal, hex, octal or as character.

  27
  1b
 033
 esc
 127
  7f
 177
 del
0
2

Simply use this command:

showkey -a
3
  • Thanks! I tried showkey for a while, neither showkey nor sudo showkey worked, but -a is the magic flag.
    – Steven Lu
    Oct 26, 2021 at 17:26
  • it would just spit back Couldn't get a file descriptor referring to the console. Might you know what this means?? @egmont
    – Steven Lu
    Oct 26, 2021 at 17:26
  • 1
    showkey was primarily designed to work on the Linux text console (i.e. Ctrl+Alt+F1 and friends). All options but -a look for extra features only available there.
    – egmont
    Oct 26, 2021 at 19:42
0

I found a trick elsewhere to show a single character in bash by using read -rsn1 key && echo "$key" | xxd. This was 90% of the way there but I was stuck on this for a while because I would just get back 0x1b (Escape), which wasn't the full picture.

Then I changed it to read -rsn2 key && echo "$key" | xxd and this scales up to however many characters your escape sequence happens to have, a little bit of experimentation does the trick. So on my terminal I had both Ctrl+ and Alt+Backspace mapped to 0x1b, 0x7f (meta-backspace if you will) in the past for convenience reasons. I am going down this path in the first place because I want to configure a new terminal emulator to let me delete words with both combos but only one of them works. The read -rsn in bash is a good tool for doing a lower level of output debugging than cat.

1
  • 1
    You'd want IFS= read -rsn2 key for it not to mangle the characters of $IFS, and replace echo with printf %s so a 0x0a byte be not added and backslash not mangled. Note that read -nx reads up to x characters (not bytes) or up to a newline. Use LC_ALL=C IFS= read -rsN2 to read 2 bytes (including newline). That still doesn't work for 0 bytes though. Oct 26, 2021 at 7:50

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.