After a low of trial and error, googling and and man-page reading, this is what I ended up doing.
Additionally to my question, I had the following requirement:
I need to be able to hide multiple panes and distinguish between them when unhiding. This means that if I hide a pane, tmux needs to store the relevant information for the hidden pane in a way, that when I unhide a pane, it knows to restore the pane that was originally below the "current" pane.
That being said, this is my solution in ~/.tmux.conf
:
bind h run-shell "tmux setw $(echo @HIDDEN_PANE_#{pane_id}|sed -e 's/%//g') $(tmux break-pane -dP)"
bind H run-shell "tmux join-pane -hs $(tmux showw -v @HIDDEN_PANE_$(tmux run-shell \"echo '#{pane_id}+1'|sed -e 's/%//g'|bc\"))"
EDIT: Thanks to @NicholasMariott for the comment about user options (I was using environment variables before).
With Prefix-:-h I can hide a pane. It stores the relevant info from break-pane (which hides the pane) in a window user option (options starting with @ are user options), so the variable is window specific. To distinguish between the panes, the user option makes use of the tmux variable #{pane_id}
which starts with a %
followed by the numeric ID of the pane. To not run into issues, the %
character is removed from the id. So the pane information from break-pane is stored in an option like @HIDDEN_PANE_2
if pane %2
was hidden.
To restore the pane with Prefix-:-H I use tmux showw -v
to show the option. But in this case, it constructs the variable name for the "current" #{pane_id}
+1, which should be the id of the pane "below" the current one, so that is the one to restore. bc
is used to do the actual addition.
This seems to work for me, even though it is quite a monstrosity :) Any comments are welcome and I'll try to include them in the answer.
Possible improvements are to put it in a separate bash script that I run from tmux.conf with run-shell. In there I could then also test for vertically vs horizontally split panes, (using #{window_height} - #{pane_height}
) and adjust the join-pane accoringly (join-pane -vs
versus join-pane -hs
).
Also the variable names could be adjusted to be more robust like by including the window/session ids.