60

This is a bit of my tmux.conf

cat tmux.conf
...
bind a set-window-option synchronize-panes on
bind b set-window-option synchronize-panes off

As you can see, sync and unsync options for panes are bound to two different keys. Is it possible to toggle sync/unsync with the same key?

facha
  • 11,862
  • 14
  • 59
  • 82
  • Tmux does not enable this by default. It can be done by writing a script, but it's not trivial. –  Sep 19 '14 at 11:59

5 Answers5

96

If you don't explicitly specify "on" or "off", the option will get toggled. The following would suffice:

bind-key a set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized,on,off}"
lleaff
  • 4,249
  • 17
  • 23
Ojp
  • 976
  • 6
  • 3
  • 2
    Very useful, I modified this answer to show the state with `display-message`. Further info variable substitution and conditionals can be found under the FORMATS section of `man tmux`, and just below that it lists the variables that are available, which `pane_synchronization` is the one we want. This is very helpful for those who use https://github.com/davidscholberg/tmux-cluster. – Elijah Lynn Apr 06 '18 at 00:25
  • *`pane_synchronized` (the code was right but my comment was incorrect and it is too late to edit it) – Elijah Lynn Apr 27 '18 at 05:12
  • Here is a link to a man page with the FORMATS section: http://man7.org/linux/man-pages/man1/tmux.1.html#FORMATS . For whatever reason, the first 2 man pages of tmux that I looked at didn't HAVE a formats section! Lame! I had to use the query `man tmux "FORMATS"`, with the quotation marks around FORMAT, to search for it explicitly. – Ari Sweedler Mar 08 '19 at 06:57
  • Unfortunately it can't bethissed to scroll in two panes at the same time since it's only for sending keystrokes into the panes, not imulatenously do tmux commands targetted at them. – Moberg Mar 04 '21 at 10:17
5

don't attach on or off. it's toggle bind-key a set-window-option synchronize-panes

junho85
  • 71
  • 1
  • 4
3

This should be possible with a combination of run-shell and tmux show-option, something like (tested in tmux 2.3):

bind a run-shell "if [[ `tmux show-options -w | grep 'synchronize-panes.*on'` ]]; then toggle=off; else export toggle=on; fi; tmux display-message \"sync panes tmux: \$toggle\"; tmux set-option -w synchronize-panes \$toggle &> /dev/null"

(this is a variation of a mouse-mode toggle found on the TMux user mailing list)

Tomáš Diviš
  • 906
  • 8
  • 9
Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
  • Thanks for the link for mouse-mode toggle. Just looking for this. Here's the mouse toggle I settled with: bind m run-shell "if [[ `tmux showw | grep mode-mouse.*on` ]]; then toggle=off; else export toggle=on; fi; tmux display-message \"Mouse: \$toggle\"; tmux setw mode-mouse \$toggle &> /dev/null; for cmd in mouse-select-pane mouse-resize-pane mouse-select-window; do tmux set -g \$cmd \$toggle &> /dev/null; done;" – Ted Feng Dec 23 '14 at 06:30
  • Ojp's answer is simpler, but it didn't work for me for the mode-mouse setting in tmux 1.8, perhaps because the setting can take a value other than on/off (on/off/copy-mode). The method here does work for that case. – ws_e_c421 Sep 12 '17 at 17:35
1

Here's an example of toggling the mouse on and off with ^M:

bind-key c-M  set-option -g mouse \; display-message 'Mouse #{?mouse,on,off}'
Elliptical view
  • 3,338
  • 1
  • 31
  • 28
0

A more generic solution based on the answer by Frank Schmitt:

!/usr/bin/bash

USAGE="USAGE: $0 OPTION_NAME ON_STATE OFF_STATE"

OPTION_NAME=$1
ON_STATE=$2
OFF_STATE=$3

if [[ "$#" != 3 ]]; then
  echo $USAGE
  exit 1
fi

if [[ `tmux show-option -w | grep "$OPTION_NAME $ON_STATE"` ]]; then
  OPTION_VALUE=$OFF_STATE
else
  OPTION_VALUE=$ON_STATE
fi

tmux display-message "monitor activity: $OPTION_NAME $OPTION_VALUE"
tmux set-option -w $OPTION_NAME $OPTION_VALUE > /dev/null

The script takes the name of the option, the on value and the off value. Not very well tested but works for simple cases like:

PATH_TO_SCRIPT_ABOVE monitor-activity on off

In your .tmux.conf:

bind-key <SOME_KEY> run-shell "tmux_toggle_option monitor-activity on off"
Serg
  • 316
  • 2
  • 14