GNU Screen – How to Search for a Suitable Command Character

gnu-screenkeyboardkeyboard shortcuts

I'm having a hard time finding a "command character" for GNU-screen that does not collide with some key combination that I already use.

Through Emacs (and org-mode, and my regular shell, etc.), pretty much all the command characters consisting of Ctrl + [A-Z] are off-limits.

I figure that, if there were a simple, and consistent, way to type any one of the characters in the ASCII range 27 through 31, that may do the trick.

Are there any tools I could use to find a way to type one of these characters that fits the above criteria?

(BTW, by "consistent" I mean that it can be typed the same way in "all keyboards", or at least in "all US keyboards".)

Best Answer

You can use any function key that Screen recognizes as the escape character. Set the escape character (more precisely, the escape byte) to one that you never type, for example \377 which is never used by UTF-8. Bind the key you want to use as the escape key to command, for example F12 (which is F2 in termcap speak — see the section “Input translation” in the Screen manual):

escape \377\377
bindkey -k F2 command

You aren't limited to keys for which Screen knows a symbolic name. To set an arbitrary key or keychord as the command key (the key does need to send an escape sequence in the terminal), find out what escape sequence it sends by pressing Ctrl+V then that key at a shell prompt. This causes the escape sequence to be inserted literally, instead of being recognized by the shell as an escape sequence. For example, if you wanted to use F12, you could press Ctrl+V then F12, which inserts ^[[24~ (the first ^[ represents the escape character) (some terminals may send a different escape sequence); then you would use

bindkey ^[[24~ command

I use Ctrl+\ as my Screen escape key (escape ^\\\), and let Ctrl+\ \ inject a ^\ character. Few programs use the ^\ control character. In Emacs, it calls toggle-input-method, which I don't use; your mileage may vary. In a cooked terminal, it sends SIGQUIT, which is rarely useful (when it's needed, Ctrl+Z followed by the kill command usually works as well; I default to having core dumps disabled). Ctrl+\ can be difficult to type on some keyboard layouts where \ requires AltGr, but on the US layout it's straightforward. I also configure my terminal emulator to send the ^\ character when I press Ctrl+`, which lets me type it with the left hand as well.

Ctrl+] (Emacs: abort-recursive-edit) and Ctrl+^ (Emacs: undefined; but it's awkward to type on a US keyboard as well as on many other layouts) are other likely candidates for an Emacs user.


By the way, if you only use Screen to run Emacs and some shells, you don't need Screen. Emacs can play the same role. Keep a single Emacs process running, and use emacsclient to open a window on this Emacs process from any terminal. If the terminal goes away, you can run emacsclient again to connect to the same Emacs instance. You can have as many instances of emacsclient as you like that are connected to the same Emacs instance, possibly displaying through different local and remote connections.

To start Emacs with no interface, run emacs --daemon. Then run emacsclient -c to open a window to the existing Emacs instance (or emacsclient -c -nw to force a text mode window). If you want to start Emacs if it wasn't already started but connect to the existing Emacs instance if there is one, run emacsclient -a '' -c.

If you want to run shells, you can use M-x shell or M-x term or the like inside Emacs. Emacs can do pretty much everything Screen can do.