0

I have started using vi mode in bash (set -o vi), and my laptop has no escape key (stupid touch bar…) so its annoying to leave insert mode on the command line. This got me thinking that in Vim, I remapped Ctrl-C to the same functionality as the escape key.

Now since Ctrl-C has a very important use in the shell usually, mapping that key combo to Esc may not be a great idea, but out of curiosity:

Is it possible to map Ctrl-C or Ctrl-$something to perform the same as the Escape key in readline?

5
  • 1
    Ctrl-C is handled by the terminal driver before reaching bash or readline. readline doesn't turn off the isig tty setting. But why not just use Ctrl-[ instead of Escape (that's what I do on my old QWERTY phone)
    – mosvy
    Jan 14 '20 at 16:53
  • 1
    Have you considered CapsLock? It's a large key right on the home row and its nominal function isn't particularly useful.....
    – mattdm
    Jan 14 '20 at 16:59
  • So far I haven't found an easily digestible resource on the syntax of readline settings. How would rebinding Caps lock look like?
    – oarfish
    Jan 14 '20 at 17:11
  • @mattdm You cannot rebind the CapsLock in readline, since CapsLock isn't sending any character sequence that could be bound or rebound. And how that could be done in a terminal emulator, linux vt or X11 should be a different Q.
    – mosvy
    Jan 14 '20 at 17:26
  • That's true -- I'd recommend doing it at the terminal or input system level, not as a readline binding.
    – mattdm
    Jan 14 '20 at 17:31
1

For the Ctrl-$something part, you can use Ctrl-[ instead of Escape in readline or the terminal in general without any extra configuration.

You can also bind any of Ctrl-@, Ctrl-[A-Z], Ctrl-[, Ctrl-\, Ctrl-], Ctrl-^ and Ctrl-_ (provided they're not a terminal special character like Ctrl-D => eof or Ctrl-\ => quit) with eg:

bind '"\C-@": "\e"'

On some terminals, Ctrl-@ is also generated by the Ctrl-`‎ combo, and Ctrl-_ by the Ctrl-/ combo.

Also, the Alt-key combinations are usually mapped to Esc-key in most terminals (subject to terminal emulator settings), so you can bind for instance Alt-C instead of Ctrl-C to Escape with:

bind '"\ec":"\e"'

For the Ctrl-C part, Ctrl-C is handled as a special key (intr) by the tty driver before even reaching bash or readline, since readline doesn't turn off the ISIG tty setting when entering the line editor. You can of course, always do eg.

stty intr ^]; bind '"\C-C":"\e"'

and retrain you to use Ctrl-] instead of Ctrl-C when you want to interrupt a job, but that's quite obviously a very bad idea.

3
  • 1
    Unfortunately i am not using US layout, so the ] is very cumbersome.
    – oarfish
    Jan 14 '20 at 17:09
  • What would be the syntax for using some arbitrary key for $something?
    – oarfish
    Jan 14 '20 at 17:09
  • @oarfish The Ctrl-] was obviously just an example. Readline can only rebind character (sequences) which are available in the terminal (no CapsLock, etc). If you want to rebind keys at system or terminal emulator level, then please ask another Q and mention all the needed details (which terminal are you using, etc).
    – mosvy
    Jan 14 '20 at 18:55

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.