General approach
The select-layout
tmux command allows you to specify a layout in the format used by window_layout
or window_visible_layout
tmux variable. This leads to the following solution:
One-time preparation
In a new tmux window manually create the layout you want.
In a shell inside one of the panes in the tmux window query tmux for
window_layout
and save the response to a file:tmux display-message -p '#{window_layout}'>my_layout
Usage
Any time you want to use the layout, pass the content of the file to
select-layout
:tmux select-layout "$(cat my_layout)"
Notes:
select-layout
allows you to select a target pane (with-t target-pane
), so you can use the command to select a layout for another window or even from the outside of tmux.Instead of a file you can use a shell variable, but since it's virtually impossible to change variables or the environment of an already running shell from the outside of the shell, and managing the environment in tmux is somewhat complicated, it's relatively easy to find yourself in a situation when you need the variable in a shell where it's not set. Files seem a better approach; e.g. you can create a directory
~/.tmux_layouts/
and store your custom layouts there.A layout created for N panes can be selected for a window containing up to N panes.
A layout is saved from a window of certain dimensions. Selecting the layout for a window of different dimensions will make tmux recalculate it to the new dimensions. The result may or may not be exactly what you want, but at least the general arrangement of panes should match and you should be able to achieve the desired exact result by resizing the panes (e.g. by dragging their borders with your mouse).
Layouts that look identical, may or may not be logically identical. E.g. the panes in this layout
+-----+-----+| | || | |+-----+-----+| | || | |+-----+-----+
can be resized either to
+----+------+| | |+----++-----+| | || | || | |+-----+-----+
or to
+-------+---+| | || | |+-------+ || +---+| | |+-------+---+
In other words either the vertical or the horizontal border is a single entity that reaches from edge to edge, while the perpendicular border is in fact two borders that can be moved independently. Which exact layout you have, it depends on if you did the vertical or the horizontal split first while creating the layout. For more complicated layouts there are more possibilities.
The differences manifest themselves not only when panes are resized. Visually identical but logically different layouts may give different results if selected for a window with fewer panes than in the layouts.
In general you may want to carefully design your desired layout (I mean the order of splits), so it behaves as you expect when you resize panes (drag borders) or select it for a window with fewer panes.
Specific example
The following command arranges 8 panes to what I think the OP meant. When selected for a window with fewer panes, it may or may not match the expectation though.
tmux select-layout '0ab5,208x60,0,0[208x15,0,0{104x15,0,0,12,103x15,105,0,29},208x14,0,16{104x14,0,16,28,103x14,105,16,30},208x14,0,31{104x14,0,31,22,103x14,105,31,31},208x14,0,46{104x14,0,46,27,103x14,105,46,32}]'
While the general approach (above) advises saving to and reading from a file, this example specifies the layout directly. I created the "layout code" in tmux 3.3a.