1

Problem description

I would like to bind commands which I execute very often in a terminal to key combinations. The bind command appears to work at first.

However, when I use the key combinations, eventually the letters I type in the terminal are invisible and pressing enter does not create a new line. My guess is that it may be somehow related to the less pager.

stty --all executed before and after the problem starts reveals the terminal line setting differences:

  • echo -> -echo (echo input characters) This is now off, so everything I type is invisble.
  • icrnl -> -icrnl (translate carriage return to newline)
  • icanon -> -icanon (enable special characters: erase, kill, werase, rprnt)
  • lnext = ^V -> lnext = <undef> (CHAR will enter the next character quoted) It is no longer possible to type ctrl+v and then, for example, Esc to enter the escape character.
  • flush = ^O -> flush = <undef> (https://man.openbsd.org/stty.1#flush)

Why does this happen? Is it possible to have custom terminal key combinations without the invisibility problem?

How to reproduce

I have tested it in multiple terminals, including lxterminal, kitty and alpine.

  • Bind a key combination, e.g. bind -x '"\ea":"ls"' binds Alt+a to the ls command.
  • View a file with less
  • Exit the pager with Ctrl+c
  • Press Alt+a. This should execute the ls command.
  • View a file with less again
  • Exit the pager with Ctrl+c
  • Press Alt+a. This should execute the ls command again. After that typed letters are invisible.

I do not know why the problem only happens the second time I press Alt+a after exiting less with Ctrl+c, and why it happens at all.

Here's a video of the problem: unfortunately webp and videos are unsupported

  • Not a solution, but maybe using `xbindkeys` would be a valid workaround? Have a look at my answer [here](https://unix.stackexchange.com/a/91433/22222) where I explain how to use it for something else, but the explanation should work for your case too. – terdon May 02 '23 at 19:35
  • Don't exit `less` with `Ctrl+C`. It leaves the terminal in an unclean state, which should be fixable by running `reset`. It has probably absolutely nothing to do with `bind`. Quit `less` by pressing `Q`. – egmont May 03 '23 at 05:14
  • @terdon, I assume `xbindkeys` key bindings work globally and not only in the terminal, so I cannot use them to execute commands except in a hacky way with `xdotool`. – user13062187 May 03 '23 at 18:15
  • @egmont, it also happens if I don't exit the pager with `Ctrl+c`. On my system, if I execute `git log` and then press `Ctrl+c`, it does not exit. If I execute `git log`, press `Ctrl+c`, press `q` to exit the pager, press `Alt+a` (the binding), and then do the same again, there are invisible characters again. – user13062187 May 03 '23 at 18:22
  • @user13062187 ah yes, good point. – terdon May 03 '23 at 18:31
  • @user13062187 I recommended you not to press Ctrl+C, and then you come up with different examples that involve Ctrl+C. Do _not_ press Ctrl+C _at all_ if `less` is involved. Get used to quitting `less` by pressing Q. – egmont May 03 '23 at 22:08
  • @egmont, you recommended to not exit less with `Ctrl+c` but not against pressing `Ctrl+c` without exiting. If less does not exit when I press `Ctrl+c`, does it catch the SIGINT signal? And if so, why is the terminal in an unclean state nonetheless? – user13062187 May 18 '23 at 18:56
  • Do _not_ press `Ctrl+C`. Press `Q`. That will quit `less`. In turn, the app that was feeding its data to `less`, if still running, will receive a SIGPIPE and also quit. But you shouldn't care about this under the hood stuff. Just press `Q`. That's the gentle way of quitting, `Ctrl+C` is harsh. I don't know why exactly it leaves the terminal in an unclean state, we'd probably need to study `less`'s source to figure it out, but apparently it does. If you are _using_ `less` in any way, forget `Ctrl+C`, get used to pressing `Q` instead. Not sure how I could be any more clear on this. – egmont May 18 '23 at 21:35
  • @egmont, the problem also happens if I use `Q` to quit the pager and never press `Ctrl+C` (and in this case different steps are needed to reproduce the problem). – user13062187 Jun 10 '23 at 09:33
  • Are you sure it has anything to do with the `bind` command and the `Alt+A` keypress? Is the problem not reproducible if you type the actual command without such shortcuts? Also, what are those "different steps" that you've just mentioined? – egmont Jun 10 '23 at 11:55
  • When I type the actual command or an alias instead of a bound key shortcut, the problem does not happen. The steps to reproduce the problem without `Ctrl+C` only work on a certain Ubuntu system and I couldn't generalise them yet to other operating systems. – user13062187 Jul 12 '23 at 20:13

0 Answers0