You can save and restore your tmux pane layouts, but it’s not about "templates" in the sense of pre-defined blueprints. It’s about capturing the exact current state of your tmux windows and sessions, including pane sizes, positions, and even the commands running inside them, and then replaying that state later.
Here’s tmux in action, demonstrating a common workflow.
First, let’s set up a few panes. We’ll start with a single pane, split it vertically, then split one of those panes horizontally.
# Start a new tmux session
tmux new-session -d -s my_layout_test
# Split the first pane vertically
tmux split-window -v
# Split the right pane horizontally
tmux split-window -h -t 1 # -t 1 targets the second pane (index 1)
# Select the first pane (top-left) and run a command
tmux select-pane -t 0
tmux send-keys "htop" C-m
# Select the second pane (bottom-left) and run a command
tmux select-pane -t 2
tmux send-keys "tail -f /var/log/syslog" C-m
# Select the third pane (right) and run a command
tmux select-pane -t 1
tmux send-keys "docker ps" C-m
Now, let’s see what we have. If you were attached to this session, you’d see something like this:
+-------------------+-------------------+
| htop | docker ps |
| | |
+-------------------+-------------------+
| tail -f /var/log/ | |
| syslog | |
+-------------------+-------------------+
This is a perfectly arranged set of panes for a specific task, like system monitoring. We’ve got htop in the top-left, docker ps in the top-right, and tail -f /var/log/syslog in the bottom-left.
The core problem tmux layouts solve is ephemeral complexity. You spend time arranging your tmux environment for a particular task – maybe a development session with your code editor, a REPL, and a build command. When you close that session, or your machine reboots, all that painstaking arrangement is gone. You’re back to a single, blank pane. Restoring it manually is tedious and error-prone.
This is where saving and restoring comes in. tmux can dump the state of your current session, or specific windows, into a file. This file isn’t just a list of commands; it’s a precise description of the tmux environment.
The primary commands for this are show-environment (to see what’s available) and save-session and load-session. However, the most common and powerful way to do this is by scripting list-windows and list-panes to reconstruct the session.
Let’s save the current layout.
# Save the current window's layout to a file
tmux list-windows -F '#{window_index} #{window_name}' | while read win_idx win_name; do
echo "Saving window: $win_idx $win_name"
tmux save-window -t $win_idx > ~/.tmux/layouts/$win_name.tmux
done
This script iterates through each window in the current session and saves its layout to a file named after the window’s name (e.g., ~/.tmux/layouts/0.tmux). The save-window command captures the pane structure, sizes, and importantly, the commands running in each pane.
Now, to restore.
# Restore a saved window layout
tmux source-file ~/.tmux/layouts/0.tmux
When you source-file (or source as it’s often abbreviated) a .tmux file generated by save-window, tmux reads the commands and re-creates the window exactly as it was. It will re-split panes, resize them, and crucially, re-execute the commands that were running in each pane. It’s like hitting a "save state" button for your entire tmux workspace.
You can also save an entire session.
# Save the entire session
tmux dump-session -LW -t my_layout_test > ~/.tmux/sessions/my_layout_test.tmux
And restore it:
# Restore the entire session
tmux source-file ~/.tmux/sessions/my_layout_test.tmux
The dump-session command is more comprehensive, capturing all windows, their layouts, and their states.
The real power comes when you integrate this into your workflow. You might have a set of common development environments, each saved as a .tmux file. For instance, you could have ~/projects/my_app/backend.tmux, ~/projects/my_app/frontend.tmux, and ~/projects/my_app/logs.tmux. Then, with a simple tmux source-file ... command, you can instantly recall a complex, multi-pane setup.
What most people miss is that save-window and dump-session don’t just save the structure of your panes, but the exact state of the shell within each pane. This includes the current working directory, the command history (if the shell was configured to save it), and the running process itself. This means if you save-window while htop is running, sourcing that file will re-launch htop in that pane. This is incredibly useful for resuming work precisely where you left off.
This mechanism also forms the basis for more advanced tmux scripting and plugins that manage complex workflows, allowing you to switch between vastly different workspace configurations with a single command.
The next logical step is to manage these layout files automatically, perhaps triggered by entering a project directory.