This would likely best be done by calling an external script (probably
with a fully qualified path, e.g. /home/user/libexec/tmux-foo) as
otherwise one must deal with a layered nest of tmux, sh, and awk
interpolation and quoting rules. Fun!
Also the command is too complicated, especially given that it is broken
in several ways, most notably that the internal " must all be escaped
with \", and maybe some of the $variables also need \$variable
escaping to protect them from tmux interpolation.
So we need something simple, and a means to reload the tmux
configuration to ensure that we get the new configuration, and to
confirm that tmux does not yell at us about one of the many typos that
you will make (unless you make less typos than I do). Also I already
have R for reload so I put the special command on W, which you might
want to change around.
bind-key R source-file ~/.tmux.conf \; display-message "source-file done"
bind-key W run-shell "tmux list-panes -F \"#{pane_id} #{pane_mode}\" >> SOMEOUTPUTFROMTMUX"
With this in place we can then run R W and confirm there are no
syntax errors tmux reports, and then find the SOMEOUTPUTFROMTMUX
file, which may be under your home directory, or otherwise should be
fairly easy to find. In another terminal, we can watch this file as we
fiddle around with the tmux configuration:
tail -f SOMEOUTPUTFROMTMUX
I've also simplified the tmux command as #{line} was or was not
present depending on how I ran the tmux command, and columns that move
around make selecting the right column with awk a little difficult.
Also grep ... | awk ... can usually be reduced to just a more
complicated awk ... expression, which shortens our command and saves
some CPU time.
bind-key W run-shell "tmux list-panes -F \"#{pane_id} #{pane_mode}\" | awk '!/tree-mode/{print $1}' >> SOMEOUTPUTFROMTMUX"
Be sure to test with R W often in tmux; trying to do the whole
thing at once is complicated. Testing after each new shell command lets
you know if you're on the right track or not. The above excludes the
tree-mode stuff.
bind-key W run-shell "tmux list-panes -F \"#{pane_id} #{pane_mode}\" | awk '!/tree-mode/{print $1}' | while read id; do echo $id >> SOMEOUTPUTFROMTMUX; done"
Whoops, the output file is empty. tmux apparently does something with
the unquoted $id, so we quote it with \$id
bind-key W run-shell "tmux list-panes -F \"#{pane_id} #{pane_mode}\" | awk '!/tree-mode/{print $1}' | while read id; do echo \$id >> SOMEOUTPUTFROMTMUX; done"
but \$id in turn must be quoted to prevent a POSIX shell from doing
crazy things with it otherwise, but not single-quoted, because we do
need the shell to interpolate it:
bind-key W run-shell "tmux list-panes -F \"#{pane_id} #{pane_mode}\" | awk '!/tree-mode/{print $1}' | while read id; do echo \"\$id\" >> SOMEOUTPUTFROMTMUX; done"
Anyways, following this method it should be possible to add the other
tmux variables back in, and then to get the sort command working
correctly, and only after the echo results look good replace the
caveman debugging (printing output to a file) with the actual tmux
command to be run:
... while read id; do tmux respawn-pane -k -t \"\$id\"; done
Or you could do this in an external script, which gives you a file
that's a bit easier to test and does not run afoul tmux and sh and
awk interpolation and escaping rules.