Making Game: What’s the least conflicting prefix/escape sequence for screen or tmux?

Original Source Link

Screen goes with Ctrl+A. tmux on the other hand—as developed within screen—uses Ctrl+B. Both keystrokes, however, are also used in editors, shells, etc. Thus choosing either one degrades the user experience and functionality of those tools when used within tmux or screen.

What prefix conflicts the least with other programs’ and shells’ keybindings?

I think ^ (a.k.a. ^|) is the best if it’s in a convenient position on your keyboard layout. Its uses in other programs are quite rare:

  • sending SIGQUIT to a process
  • aborting for or while loops in a shell when ^C is intercepted
  • toggle-input-method in emacs

I don’t know of any other uses. Be careful to not accidentally kill a processes outside of tmux or screen after getting used to it. It happened to me only once in a few years though.

~/.tmux.conf

unbind-key C-b
set -g prefix 'C-'
bind-key 'C-' send-prefix

~/.screenrc

escape ^|^|

For people who want to use backtick as the escape in tmux, you’ll want to add:

unbind C-b
set -g prefix `
bind-key ` send-prefix

That last one is important, else you can’t type a backtick for other purposes 🙂

Ctrl+A is also known to cause problems with Emacs, including Bash in Emacs mode. It sounds like this is not a problem for you.

Ctrl+O is the other option I’ve seen. Apparently, this is the default in RatPoison (this is an X window manager that doesn’t need a mouse). I’ve used Ctrl+O when using nested screens: Ctrl+O for the outer one and Ctrl+A for the inner ones. Worked well, but kinda scared my colleagues. 🙂

I was just thinking and if you use vi rather than Emacs, there are a few alternatives. Ctrl+G isn’t used by much, for instance.

I’ve got CAPS-LOCK globally mapped to ESC. Then, I use M-Space (ie. CAPS-LOCK+ Space) for my prefix.

I use Ctrl-Q in tmux and it has worked well so far. I have to mention, though, that it conflicts with shell flow control. By default, Ctrl-Q is used to re-enable output after stopping it with Ctrl-S. Having been surprised by a stuck shell a few times after accidentally hitting Ctrl-S, I have learned not to press Ctrl-S.

(One could also turn off flow control altogether with stty -ixon, or bind different keys to stty start and stty stop.)

# tmux.conf
unbind C-b
set -g prefix C-q

I use a complex system for screen. My default escape is set to 140140, which is backtick. The Ctrl-A complicates both Emacs and command line editing for me within Zsh, and I dislike Ctrl-O (2 hand operations for most screen actions).

I rebind ‘s’ to screen 1 so that new sessions are created from left to right on the keyboard starting at 1. This allows me to reserve screen 0 for what I consider persistent or reference windows. It’s very quick one handed gesture to (backtick)1, (backtick)2, (backtick)3 to swap between windows.

The issue with using backtick in a Unix environment is when attempting to cut-and-paste shell/Perl script code. For this reason I bindkey F11/F12 to switch between my escape character.

bindkey -d -k F1        escape ^O^O # bound to F11
bindkey -d -k F2        escape 140140 # bound to F12

This will swap the escape to Ctrl-O for when I’m doing cut-and-paste operations. I’ve found hitting a double tick is simple, and a good trade off for most screen operations.

Revisiting this answer with a newer solution that allows for toggling the mode by hitting F12, and using a caption to indicate mode.

## command characters
escape 140140                # default is `

## sets caption and escape toggle
bindkey -d -k F2 process a     # bound to F12

## initial caption
caption always '%{= kW}%?%F%{+b KW}%:%{= kK}%? %= %?%F%{-b .c}>>>%{-}%? | %-w%{mW}%n* %t%? @%u%?%{-}%+w '

## registers to toggle bindkeys
register a "140:eval 'bindkey -d -k F2 process b' 'process c' 'escape \017\017'^M"
register b "17:eval 'bindkey -d -k F2 process a' 'process d' 'escape \140\140'^M"

## registers to change captions
register c "140:caption string '%{= kW}%?%F%{+b mW}%:%{= kK}%? %= %?%F%{.c}ALT%{-}%? | %-w%{KW}%n* %t%? @%u%?%{-}%+w '^M"
register d "17:caption string '%{= kW}%?%F%{+b KW}%:%{= kK}%? %= %?%F%{.c}>>>%{-}%? | %-w%{mW}%n* %t%? @%u%?%{-}%+w '^M"

I like to reserve ^Space for very special/common operations because I find it to be the easiest prefix to type, but right now I’m trying it mapped as the prefix in tmux.

It leaves your fingers free to instantly jump to the command you want to type. Give it a try.

A belated suggestion: ctrl-s. ctrl-s has a number of advantages:

  1. On the home row.
  2. Still close to ctrl-a (in fact, for most typers it will use the same two fingers they used for ctrl-a), so the muscle memory switch from ctrl-a to ctrl-s is trivial — for me, it became second nature within about an hour of first trying it.
  3. Frees up ctrl-a for emacs-style “back to beginning of line” or vim-style “increment number” operations. Or hey, for running screen inside a tmux pane without needing to worry about escaping prefix characters to control the embedded screen instance. (I often do this using a local tmux with panes containing ssh sessions to remote servers, in which I run screen)
  4. Doesn’t override or add escaping-requirements to any other commonly-used terminal functionality. nothing the vast majority of people use today is on ctrl-s!

Of course, the reason that nothing is on ctrl-s is that in the terminal, it traditionally is used for flow control, dating back to the days before paging tools like more and less were common. I’m sure some GUI terminal program somewhere still has that flow-control functionality enabled by default, but I haven’t actually bumped into one; the gui terminal programs I’ve tried all seem to completely ignore it by default, which makes that convenient key combination available for more productive uses.

So if you’re not actually using screen/tmux from within (for example) a raw Linux terminal, but rather from a GUI-based terminal, then I recommend giving ctrl-s a try; it’s made zipping about in tmux and screen a lot more convenient for me.

A key without conflict may be C-z, as it is usually used to send the program to the background to continue another thing on the shell that started it. I don’t do this when I have tmux.

I’ve heard of ` (backtick) being used and then you just have to type it twice for an actual backtick. Might be better for vi users who are used to the action (unless you do the Caps lock thing).

I recommend C-j, which doesn’t interfere much with vi or emacs. Essentially, I agree with a comment by Susam Pal, which seems worthy of showing up as an answer, since the others all seem to have significant conflicts from my perspective.

I use C-j as my prefix key since C-j is synonymous to j or in
Vim and nobody uses C-j to move one line down in Vim. I have this in
my ~/.tmux.conf:

set -g prefix2 C-j; bind-key C-j send-prefix -2

For Programmer Dvorak keyboard layout, it is pretty neat to use a minus (dash) key as a prefix, i.e. ctrl+-.
The minus key is on single quote key position in qwerty layout. Also I’ve caps lock key function as a ctrl key.
Following is a tmux conf, to set it as a prefix:

unbind C-b
set -g prefix C-_
bind C-_ send-prefix
bind - last-window

You may wonder, why the underscore is used instead of minus? Refer to Tmux: how do I bind to C— (control-minus)?
For qwerty, you might apply How do I bind the tmux prefix key to C-‘? solution, in order to retain same shortcut.

I would say this is a perfect prefix if you use Vim/Neovim. Because prefixes, such as ctrl+a or ctrl+b, do shadow (overlap) Vim’s functionality, in either way.

Personally, b is just too far away from Ctrl for me. When I use tmux, I alway change the binding from C-b to C-a. The main conflict with C-a is select all, but I have not found this to be a problem with the programs I use tmux with.

As a GNU emacs, zsh, and MS Windows user, I use Control-T. (e.g. in .screenrc:)

escape “^T^T”

Yes Control-T has something bound to it, like the pull down menu in Ubuntu’s aptitude, or transpose character in Emacs.

I disregarded C-o because it requires two hands for me.

I think the best solution is ^C. Emacs uses ^C as a prefix key, and I find that when I need to type ^C in a shell I almost always type two anyway. 🙂

unbind-key C-b
set -g prefix C-c
bind-key C-c send-prefix

I use Caps Lock, and on OSX you need to do some special stuff to get it to work.

https://gist.github.com/burtlo/3788048

You can use backtick. Some older versions of tmux do not support backtick, so you can do the following to workaround (that would add C-`, [email protected] and C-space as your meta at the same time though):

# meta prefix - @ - backtick
unbind C-b
set -g prefix [email protected]
bind [email protected] send-prefix
bind [email protected] last-window

I normally remap my CapsLock to Ctrl, so it makes even more sense with the backtick.

Most Answers here use Ctrl as part of a key combination.
When I used Ctrl, my pinkie finger started to hurt because I always had to spread it down to Ctrl.
I use Alt-j which I type with my left thumb and my right index finger and it’s very convenient. I also haven’t found any collisions with any other program so far.

unbind C-b
set-option -g prefix M-j

For GNU screen, mapping the backtick to escape, the following in ~/.screenrc works for me.

escape ``

I like the suggestion of binding ctrl- as the prefix, but in order to get this to work on Linux Mint 12, I had to escape the place the binding at the bottom. Otherwise, it wouldn’t take. See my .tmux.conf here: Using tmux with both emacs and vim

I think that Mac users will find the § more convenient than the backtick.

On non-Mac keyboard, the backtick is positioned at about the same key as the § on Mac keyboards. So, to experience the same convenience as the people who suggest to use the backtick, use the paragraph-sign on a Mac keyboard.

Using the backtick on Mac keyboards is tricky as it is located very near to the left (⇧)shift, (⌃)control, and (⌥)alt-keys. On MacBooks even near the left fn-key.

I am using Alt+F. I just added it and it is been much better then C-a or C-b. M-f doesn’t require any move from my home position (since alt is already bellow my thumbs) and it doesn’t force my pinkie finger.

unbind C-b
set-option -g prefix M-f

Tagged : / /

Leave a Reply

Your email address will not be published. Required fields are marked *