til / Maximize tmux pane vertically or horizontally
When we have multiple panes open in tmux it can sometimes be hard to focus. I tend to have one main pane for my editor and a smaller horizontal split to the right. The split on the right is used for tests, logs, runners, etc. and usually contains multiple vertical splits.
Zoom in #
We can focus one specific pane using the built-in zoom command ctrl + b
, z
, where ctrl + b
is the default tmux prefix. This is a shorthand for ctrl + b
, :
, resize-pane -Z
.
Temporary maximize #
Sometimes we may want to temporarily maximize a pane vertically or horizontally to have more space to, for instance, view tests or read logs.
To do this, we can create custom functions in our .tmux.conf
file.
bind -n M-z if -F '#{@layout_save}' \
{run 'tmux select-layout "#{@layout_save}" ; set -up @layout_save'} \
{set -Fp @layout_save "#{window_layout}" ; run 'tmux resize-pane -y 100%'}
We start by binding M-z
, that’s M
for modifier key (cmd
on Mac and alt
/esc
on Linux) plus z
. The if-statement checks if the @layout_save
variable exists, then:
- If it exists, set the layout to that value and remove the variable.
- If it does not exist, save the current layout, and resize the current pane to 100% on the y-axis (
-y
).
This means that pressing cmd + z
will toggle the current pane between 100% and its previous layout.
To perform the same action horizontally, copy and paste the function, switch to M-x
in the bind, and change -x
in resize-pane
.
# Use cmd + z (Mac) or alt/esc + z (Linux) to maximize pane size vertically
bind -n M-z if -F '#{@layout_save}' \
{run 'tmux select-layout "#{@layout_save}" ; set -up @layout_save'} \
{set -Fp @layout_save "#{window_layout}" ; run 'tmux resize-pane -y 100%'}
# Use cmd + x or alt/esc + x (Linux) to maximize pane size horizontally
bind -n M-x if -F '#{@layout_save}' \
{run 'tmux select-layout "#{@layout_save}" ; set -up @layout_save'} \
{set -Fp @layout_save "#{window_layout}" ; run 'tmux resize-pane -x 100%'}
There are some limitations to these commands since they toggle the same variable and don’t take into account when panes are removed. But, it should work for simple use cases.