What's the least conflicting prefix/escape sequence for screen or tmux?

98

53

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?

Flow

Posted 2009-11-23T21:51:56.050

Reputation: 997

6"which one conflicts least with other programs?" has nothing to do with opinion. – iconoclast – 2015-11-20T19:51:35.190

After changing ~/.tmux.conf don't forget to restart tmux server for changes to take effect (http://superuser.com/q/188491/105108).

– ks1322 – 2016-01-31T14:34:06.647

Alternative to using a prefix: Some terminals now offer tmux integration via "tmux -CC" (i.e. iTerm2 for macOS). That way tmux windows appear as tabs in the terminal. You could then use key bindings for managing tabs that don't conflict with any terminal program, i.e. by using the cmd key (aka super key, windows key). – cjay – 2019-01-30T22:09:33.487

Answers

50

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 ^|^|

cjay

Posted 2009-11-23T21:51:56.050

Reputation: 121

12but then I get in the habit of that command, and when I forget I'm in screen, I start accidentally sending SIGQUITs... – Kache – 2015-04-14T21:39:24.130

3"if it's in a convenient position on your keyboard layout" by default it's not, are you implying that there is some popular remapping that you're using which makes C-\ convenient? – storypixel – 2017-01-17T16:06:18.743

@storypixel I think it's more a matter of not assuming what people are using. Nowadays some people (like me) run custom firmware on their keyboards. – henrebotha – 2018-06-19T08:59:37.250

'C-\' is a terrible prefix key, as building muscle memory around it will cause you to inadvertently send SIGQUIT when you're using tmux's that aren't configured this way. If we're going to shadow a process control key combination, then C-z is a much more benign option. – aparkerlue – 2018-08-31T17:58:25.863

I tried this but tmux complains about the use of C-\. Can I escape the \ character somehow? – Jason – 2012-07-11T00:01:05.000

set -g prefix C-\\ was needed for 1.8 – Elijah Lynn – 2013-07-31T17:02:00.747

2Just to let somebody else know, you should write 'C-\' instead of C-\ in the tmux config. – aignas – 2014-02-14T10:46:15.230

2Don't forget to 'tmux source-file /path/to/tmux.conf' when finished to try out the new settings – user72923 – 2014-05-23T15:16:50.743

53

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 :-)

Ned Ruggeri

Posted 2009-11-23T21:51:56.050

Reputation: 181

I found this question looking for alternatives to backtick as it started to cause trouble after I got used to using backticks instead of $() in shells. Also it becomes a pain once you start writing a lot of Markdown. I've been using it for several years, but unfortunately I'll have to change it now. – kirelagin – 2014-07-01T20:09:41.683

4In .tmux.conf I also have bind-key C-a set-option -g prefix C-a. Whenever I need to use backticks I hit \-Ctrl-awhich sets my prefix toC-a. And I havebind-key C-b set-option -g prefix `` so I can hit C-a-C-b to go back – Boris – 2014-12-24T23:14:57.087

What if you're writing markdown? How do you escape the prefix? – OJFord – 2015-07-12T23:29:36.790

Ah, you double-tap it. Nice, might give it a shot. – OJFord – 2015-07-12T23:36:24.953

what about if ever write some bash? f_len=\cat file | wc -l``? – dylnmc – 2015-11-07T01:35:39.163

instead of using \`` in bash, you should try to use the more modern and more readable alternative:$(and)` – iconoclast – 2015-11-20T19:54:41.917

Don't use backticks so often, so I don't mind tapping it twice. This is my fav by far. – Vik – 2017-04-10T11:07:49.350

On a German layout keyboard, you would use the caret ^ instead. – Olaf Dietsche – 2018-08-24T21:03:17.587

Backtick seems like a good idea until you paste in some SQL... – Synchro – 2013-01-09T09:43:10.440

4

@Synchro on tmux 1.8 it's not an issue anymore thanks to assume-paste-time option which is on by default (set to 1ms). See here: http://sourceforge.net/p/tmux/tmux-code/ci/5fe0576dcbf47954957007296839e43471b088f9/tree/tmux.1#l2134

– sickill – 2013-03-27T20:46:46.587

21

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. Apparantly, 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.

staticsan

Posted 2009-11-23T21:51:56.050

Reputation: 726

C-j conflicts with CtrlP. Moving up and down the buffer list uses C-k and C-j respectively – blockloop – 2017-05-31T14:08:29.907

9For Emacs users Ctrl+O seems to be the the best pick: In emacs it´s only "open a new line at the cursor" and in bash "repeate a command sequence". – Flow – 2009-11-24T08:15:22.170

2+1 if you are OK with right handed ctrl-operations, this looks like a good backup. I'm a lefty myself ;-) – DaveParillo – 2009-11-24T16:56:16.647

9I think Ctrl+O breaks vim – Mu Qiao – 2014-03-26T14:36:32.147

14I agree with @MuQiao. Ctrl-o is used in Vim to jump to an older cursor position (equivalent of back button in many IDEs), a feature I use very often while browsing code in Vim. Ctrl-b is synonymous to PageUp in Vim. I don't use Ctrl-b at all on Vim. So for Vim users like me, Ctrl-b is still a better choice than Ctrl-o. I use C-j as my prefix key since C-j is synonymous to j or <Down> 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 – Susam Pal – 2014-04-10T09:44:17.520

14

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

Nathan Speed

Posted 2009-11-23T21:51:56.050

Reputation: 21

I'm trying to do this but if I hold CAPS-LOCK (thinking about my next command or something) tmux—or iTerm, not sure what's causing this—receives the ESC command repeatedly causing M-Space to then not work. Is there a way to disable that repeating key issue? For instance, it works when I quickly press CAPS-LOCK + Space, but not if I hold down CAPS-LOCK for a second or so and then press Space it doesn't register the M-Space chord... – Bruno Ely – 2019-04-02T17:37:00.937

12

I use a complex system for screen. My default escape is set to \140\140, which is backtick. The Ctrl-A complicates both Emacs and command line editting 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 \140\140 # 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 \140\140                 # 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 "\017: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 "\017:caption string '%{= kW}%?%F%{+b KW}%:%{= kK}%? %= %?%F%{.c}>>>%{-}%? | %-w%{mW}%n* %t%? @%u%?%{-}%+w '^M"

Darren Hall

Posted 2009-11-23T21:51:56.050

Reputation: 6 354

bind-key -n F11 set -g prefix `

bind-key -n F12 set -g prefix C-o

Updates for tmux in case others want to adapt this. – milkypostman – 2012-02-10T04:49:41.087

1Is there a way to set some kind of minimal timeout for prefix+other_key combo in tmux so when pasting code it doesn't trigger any command but when typing backtick+key from keyboard it does because the pause between backtick and the other key was longer? – sickill – 2012-05-05T16:15:16.203

Creative solution. I'm impressed. – staticsan – 2009-11-25T02:30:45.523

11

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

simonair

Posted 2009-11-23T21:51:56.050

Reputation: 624

I've adopted this bind and I find it very convenient. Thanks! – aymericbeaumet – 2013-08-23T07:29:40.073

My favorite as well, since I always disable the terminal control flow stuff. – Jack O'Connor – 2013-08-24T03:10:48.600

2Used in emacs for quote-inserted. Sample use (in emacs): if I want to search for the next tab character - <Ctrl-s>+<Ctrl-q>+<TAB> – nhed – 2014-04-08T17:15:16.697

6

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.

terrace

Posted 2009-11-23T21:51:56.050

Reputation: 295

1I also use C-Space, it is not used almost anywhere else. – mahdix – 2016-06-01T07:44:23.610

1C-space is perfect for me, as I'm on a Chromebook :D – Eric Nguyen – 2016-11-30T17:39:37.437

3That didn't last long – I'm back to ^j. ^Space is too similar to commands that I use to invoke OSX-level apps (Spotlight, QuickSilver, DTerm). – terrace – 2010-08-22T03:32:01.660

I use C-j too. This is what I use in my ~/.tmux.conf: set -g prefix2 C-j; bind-key C-j send-prefix -2 – Susam Pal – 2014-04-10T09:46:36.733

6

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.

Trevor Powell

Posted 2009-11-23T21:51:56.050

Reputation: 515

Ctrl-S is common for stopping console output (aka "freezing" it). This bit me when I tried remapping it. – Dave – 2016-09-23T20:00:51.117

4Ctrl-s is forward search, for when I go to far with Ctrl-r :) – Jack O'Connor – 2013-08-24T03:12:07.933

5

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.

Josuah Demangeon

Posted 2009-11-23T21:51:56.050

Reputation: 26

The only problem I've had with using C-z is that most other people are using C-b, so my muscle memory is off when pair programming on someone else's machine.I end up having to run fg a lot after erroneously typing C-z. – Steve Jorgensen – 2016-07-24T19:37:06.013

The great think about C-z is that, assuming you don't want to use C-a, it's very close to the left Ctrl key, which is the only control key on most notebook computer keyboards these days. Also close to the Caps Lock if you have that mapped to Ctrl. I map my Caps Lock to Esc since I'm often using a Kinesis Freestyle Solo keyboard that has the Esc key in an awkward place. – Steve Jorgensen – 2016-07-24T19:42:10.620

4

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

nealmcb

Posted 2009-11-23T21:51:56.050

Reputation: 496

1Based on your recommendation, I have been using C-j for a while and the only conflict I've found is with the CtrlP vim plugin. C-j and C-k are used to move down and up the options, respectively. – blockloop – 2017-05-26T23:59:12.957

4

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).

bluehavana

Posted 2009-11-23T21:51:56.050

Reputation: 1 046

How can you define backtick for tmux? – Alexandre Nizoux – 2011-02-08T21:16:57.997

Something like: unbind C-b; set -g prefix ''; bind '' send-prefix; see Darren Hall's answer above. – bluehavana – 2011-02-10T00:55:10.513

I'm with backtick for about a year and I'll never go back to two-key/combo prefix. Give it a try. – sickill – 2013-01-15T15:29:15.037

3

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.

Vladimir Bauer

Posted 2009-11-23T21:51:56.050

Reputation: 121

2

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

user114905

Posted 2009-11-23T21:51:56.050

Reputation: 11

2

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

Brad Parks

Posted 2009-11-23T21:51:56.050

Reputation: 1 775

2

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.

DaveParillo

Posted 2009-11-23T21:51:56.050

Reputation: 13 402

indeed, i use the default (in screen, never tried tmux) and haven't yet found a need to change it. – quack quixote – 2009-11-24T04:31:07.660

2

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.

piyo

Posted 2009-11-23T21:51:56.050

Reputation: 1 611

1

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

wullxz

Posted 2009-11-23T21:51:56.050

Reputation: 2 400

1

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

escape ``

user1056720

Posted 2009-11-23T21:51:56.050

Reputation: 1

1

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-`, C-@ and C-space as your meta at the same time though):

# meta prefix - @ - backtick
unbind C-b
set -g prefix C-@
bind C-@ send-prefix
bind C-@ last-window

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

Alex

Posted 2009-11-23T21:51:56.050

Reputation: 101

I use this as well. C-Space is quite easy to type. – kzh – 2014-08-21T16:56:27.400

The backtick is pretty good. However its not terminal friendly as it turns out. You definitely cannot go wrong with xterm on linux, but terminals like putty and iTerm2 (macos) have issues emulating it properly. Luckily in iTerm2 you can workaround it in the configuration. I don't use windows/putty a lot, so ctrl-space seems to be a feasible workaround. :) – Alex – 2014-08-23T19:02:01.880

0

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.

nanitous

Posted 2009-11-23T21:51:56.050

Reputation: 101

Define Mac keyboard ;-)

– Arjan – 2016-07-01T13:15:20.097

Okay, you've a point here, I meant a Apple Mac US keyboard layout, though regardless a full extended or a macbook keyboard. – nanitous – 2016-07-10T15:54:57.623

0

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

justingordon

Posted 2009-11-23T21:51:56.050

Reputation: 1 073

Please don't refer to answers as "above" and "below" as the order could change. Link the answer instead. Thanks for your answer :) – Flow – 2012-02-04T09:43:30.373