Is there a tmux shortcut to go read only?

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.


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;

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.