2

To balance panes in the current window, one can use Ctrl-b Meta-1..5 (1, 2). However, is there a way to perform such balancing automatically whenever a pane is added or removed?

For example, consider that I have two equally high panes in the even-vertical layout. Now, when I split the current pane into two (top and bottom), I would like tmux to automatically balance all the panes so I got three equally high panes in the even-vertical layout.

1 Answer 1

4

You can provide your own bindings consisting of commands joined by \;. For example in your ~/.tmux.conf add

bind-key '"' split-window \; select-layout -n \; select-layout -p

This makes Ctrl-b " split the window, then change the layout to the next layout, then back to the previous layout. You would need to do this for other commands that add or remove windows.


If you want more automatic control, you can write a background tmux control mode process to listen to a real tmux process. It gets notifications of everything happening in the real tmux, and you could then send select-layout commands whenever you see a notification of a new window, or a deleted window. I've not experimented much with this, but if you use 2 terminals and run a normal session in one

tmux new -s mysession

and in the other

tmux -C attach -t mysession

then when you split windows, add new ones, or close them in the normal tmux you will get lines like

%layout-change @2 91a8,80x23,0,0[80x11,0,0,5,80x11,0,12,7]
%window-add @3
%window-close @1

in the control tmux. When you see these you can write select-layout -n in the control mode, and it affects the first. There's a python library to use this mechanism, which I have not looked at further.

4
  • But what if the current layout is different from even-vertical? I would like to keep the layout that is currently being used.
    – s3rvac
    Oct 4, 2016 at 3:44
  • Finding the current layout is difficult. You can instead cycle forwards, then backwards. I updated the answer.
    – meuh
    Oct 4, 2016 at 6:21
  • Thanks. I thought that there had be a better way than rebinding all the keys, though. For example, I regularly kill a pane just by exiting the shell, which is pretty impossible to "rebind" in tmux.
    – s3rvac
    Oct 5, 2016 at 13:22
  • I've added a possible mechanism that may interest you, the control mode of tmux. I've not used it but it looks feasible, if a bit of an overkill.
    – meuh
    Oct 5, 2016 at 13:58

You must log in to answer this question.

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