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

76,196

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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
Share:
76,196

Related videos on Youtube

Flow
Author by

Flow

Updated on September 17, 2022

Comments

  • Flow
    Flow over 1 year

    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?

    • iconoclast
      iconoclast over 8 years
      "which one conflicts least with other programs?" has nothing to do with opinion.
    • ks1322
      ks1322 over 8 years
      After changing ~/.tmux.conf don't forget to restart tmux server for changes to take effect (superuser.com/q/188491/105108).
    • cjay
      cjay over 5 years
      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).
  • quack quixote
    quack quixote over 14 years
    indeed, i use the default (in screen, never tried tmux) and haven't yet found a need to change it.
  • Flow
    Flow over 14 years
    For 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".
  • DaveParillo
    DaveParillo over 14 years
    +1 if you are OK with right handed ctrl-operations, this looks like a good backup. I'm a lefty myself ;-)
  • staticsan
    staticsan over 14 years
    Creative solution. I'm impressed.
  • terrace
    terrace over 13 years
    That 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).
  • James Bay
    James Bay about 13 years
    How can you define backtick for tmux?
  • jmhmccr
    jmhmccr about 13 years
    Something like: unbind C-b; set -g prefix ''; bind '' send-prefix; see Darren Hall's answer above.
  • Flow
    Flow about 12 years
    Please don't refer to answers as "above" and "below" as the order could change. Link the answer instead. Thanks for your answer :)
  • WildTigerrr
    WildTigerrr about 12 years
    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.
  • tiadotdev
    tiadotdev almost 12 years
    Is 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?
  • biluochun2010
    biluochun2010 over 11 years
    Backtick seems like a good idea until you paste in some SQL...
  • tiadotdev
    tiadotdev over 11 years
    I'm with backtick for about a year and I'll never go back to two-key/combo prefix. Give it a try.
  • tiadotdev
    tiadotdev about 11 years
    @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: sourceforge.net/p/tmux/tmux-code/ci/…
  • Elijah Lynn
    Elijah Lynn almost 11 years
    `set -g prefix C-\` was needed for 1.8
  • aymericbeaumet
    aymericbeaumet over 10 years
    I've adopted this bind and I find it very convenient. Thanks!
  • Jack O'Connor
    Jack O'Connor over 10 years
    My favorite as well, since I always disable the terminal control flow stuff.
  • Jack O'Connor
    Jack O'Connor over 10 years
    Ctrl-s is forward search, for when I go to far with Ctrl-r :)
  • aignas
    aignas about 10 years
    Just to let somebody else know, you should write 'C-\' instead of ` C-\ ` in the tmux config.
  • Mu Qiao
    Mu Qiao about 10 years
    I think Ctrl+O breaks vim
  • nhed
    nhed about 10 years
    Used in emacs for quote-inserted. Sample use (in emacs): if I want to search for the next tab character - <Ctrl-s>+<Ctrl-q>+<TAB>
  • Susam Pal
    Susam Pal about 10 years
    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
  • user72923
    user72923 almost 10 years
    Don't forget to 'tmux source-file /path/to/tmux.conf' when finished to try out the new settings
  • kirelagin
    kirelagin almost 10 years
    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.
  • Matt Roberts
    Matt Roberts over 9 years
    I use this as well. C-Space is quite easy to type.
  • Alex
    Alex over 9 years
    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. :)
  • Kache
    Kache about 9 years
    but then I get in the habit of that command, and when I forget I'm in screen, I start accidentally sending SIGQUITs...
  • OJFord
    OJFord almost 9 years
    What if you're writing markdown? How do you escape the prefix?
  • OJFord
    OJFord almost 9 years
    Ah, you double-tap it. Nice, might give it a shot.
  • dylnmc
    dylnmc over 8 years
    what about if ever write some bash? f_len=`cat file | wc -l`?
  • iconoclast
    iconoclast over 8 years
    instead of using ` in bash, you should try to use the more modern and more readable alternative: $( and )
  • Mahdi
    Mahdi almost 8 years
    I also use C-Space, it is not used almost anywhere else.
  • Arjan
    Arjan almost 8 years
    Define Mac keyboard ;-)
  • nanitous
    nanitous almost 8 years
    Okay, you've a point here, I meant a Apple Mac US keyboard layout, though regardless a full extended or a macbook keyboard.
  • Steve Jorgensen
    Steve Jorgensen almost 8 years
    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
    Steve Jorgensen almost 8 years
    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.
  • Dave
    Dave over 7 years
    Ctrl-S is common for stopping console output (aka "freezing" it). This bit me when I tried remapping it.
  • Eric Nguyen
    Eric Nguyen over 7 years
    C-space is perfect for me, as I'm on a Chromebook :D
  • Mandrake
    Mandrake over 7 years
    "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?
  • Vik
    Vik about 7 years
    Don't use backticks so often, so I don't mind tapping it twice. This is my fav by far.
  • blockloop
    blockloop almost 7 years
    Based 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
    blockloop almost 7 years
    C-j conflicts with CtrlP. Moving up and down the buffer list uses C-k and C-j respectively
  • henrebotha
    henrebotha almost 6 years
    @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.
  • Olaf Dietsche
    Olaf Dietsche over 5 years
    On a German layout keyboard, you would use the caret ^ instead.
  • aparkerlue
    aparkerlue over 5 years
    '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.
  • sleighty
    sleighty about 5 years
    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...
  • doak
    doak almost 4 years
    Ctrl-Q is great, hitting it twice to re-enable output is not too bad.
  • doak
    doak almost 4 years
    There are a lot more in the Vim-world, like moveing windows around etc. I you are a Vim user, I would avoid remapping your cursor keys.
  • doak
    doak almost 4 years
    What about Ctrl-`? This can be bound (for my setup) by C-@ (found out with cat -v).
  • moopet
    moopet over 3 years
    @storypixel on ISO keyboards it's next to A and Z so it's as convenient as the existing screen prefix key / start-of-line, or suspend, both of which I use without issue at the moment and neither of which I wish to shadow.
  • haridsv
    haridsv over 2 years
    The only issue with using C-j for Vim is that it become harder to move to lower window. it is quite handy to use Ctrl-W_Ctrl-J. It is an alias for Ctrl-W_j, but it is so much easier to keep Ctrl key pressed instead of having to release it between the w and j keys, especially if you are moving multiple windows (try pressing Ctrl-w j Ctrl-w j Ctrl-w j instead of just wjwjwj (while keeping Ctrl pressed).
  • Robert Smith
    Robert Smith about 2 years
    Ergonomically, this would have been a good option, but Alt + F moves forward a word in Bash
  • Jhon
    Jhon about 2 years
    I use vi mode on bash/zsh, so I never used this.
  • Admin
    Admin almost 2 years
    @sleighty you can have CAPS-LOCK mapped to ESC when pressed and released and CTRL when you hold down. C-Space then works nicely as tmux prefix, but just in general it's also great.
  • Admin
    Admin almost 2 years
    I use Ctrl+G to show my file name when I am on vim to make sure that I looking at the correct log file.
  • Admin
    Admin almost 2 years
    @cviejo, how do you do this mapping based on how long you press?