1

I have been researching how to hide and unhide panes in tmux. I want to bind a key to quickly do this. With break-pane I can hide a pane, and the output of the command is the name of the pane (:window.pane if I get it right). So I want to bind a key that executes break-pane stores the output in an environment variable and then later on, to un-hide it, take the environment variable and uses it to join-pane that pane again. This is where I am so far:

bind h run-shell "export HIDDEN_PANE=$(tmux break-pane -dP)"
bind H join-pane -vs $HIDDEN_PANE

Right now, the hiding works, but how do I take the output of break-pane, and store it in the HIDDEN_PANE environment variable as part of the same bind command, so I can then use it later in the join-pane command in a different pane (and therefore shell)? Right now the environment variable doesn't get set anywhere as evidenced by Prefix+:+show-environment

1 Answer 1

3

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.

2
  • 1
    You could use user options instead of environment variables which would avoid polluting the environment of all new panes - and "show -v" will return only the value so you could leave out the sed. May 12, 2019 at 9:10
  • thanks @NicholasMarriott I didn't know that. It's still WIP so will modify May 13, 2019 at 2:23

You must log in to answer this question.

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