32

I've been using screen for years now as a way of ensuring that any remote work is safely kept open in after disconnects/crashes. In fact, as a matter of course, I use screens even when working locally.

Recently, my requirements have progressed to the stage that I switched to tmux because of the beauty of:

tmux attach -r

Attaching to my own sessions in readonly mode (-r) means that I don't have to worry about accidentally:

  • pasting lines of garbage in IRC
  • halting an important compile/deploy process
  • typing a password in full view for passersby

Of course the issue is that I have to open a session, C-b + d to detach, and then reopen it with the -r flag to go readonly. And then, when I occasionally want to chime in to an IRC conversation, interrupt a task or anything else, I have to detach again and reconnect normally.

Does anyone know of a way to make a key binding to switch between modes?

3 Answers 3

29

In tmux 1.9a you can do :switch-client -r. I'm not sure if this is new in 1.9a, but that is the version that I am running and I am able to change an existing tmux session to read-only mode.

1
  • 2
    This works great for going into read-only mode, but is there any way to exit read-only mode without detaching and re-attaching? Doesn't look like it, but that would be a useful feature. Commented Jan 5, 2019 at 19:46
24

Not according to the man page, which only calls out the attach -r option to enable read-only mode.

Also, in the source code, only the following line in cmd-attach-session.c sets the read only flag. The rest of the code checks whether this flag is set, but does not change its value. So again, it looks like you are out of luck unless you can make (or request) a code change:

    if (cmd_check_flag(data->chflags, 'r'))
        ctx->cmdclient->flags |= CLIENT_READONLY;
1
  • 1
    Thanks for going so in-depth. It has been a while since I've developed in any flavour of C/C++ but I guess I can put it in my 'someday' pile. In the meantime, it won't hurt to put a feature request in so I'm off to do that now.
    – connrs
    Commented Feb 18, 2011 at 9:31
5

In tmux 2.9a, man tmux says (under attach-session):

     -r signifies the client is read-only (only keys bound to the
     detach-client or switch-client commands have any effect)

Based on this, you can make a shortcut to go read-only (and back) by providing your own binding to :switch-client -r:

# Toggle read-only with <prefix>R
bind-key R switch-client -r

This will work when the client is read-only, too.

Note that adding an additional display-message command won't work: tmux will refuse to run the key's bound function, since it's no longer just a switch-client command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .