tmux - Resize pane to specific width and height
Quick post on how to resize a tmux-pane to a specific width and height.
I’m presuming you only have one session and that you’ve changed base-index to 1 and your prefix key combo to ctrl+a (ctrl+b is silly).
.tmux.conf
# 1 is easier to reach than 0
set -g base-index 1
# make tmux behave more screen-like
unbind C-b
set -g prefix C-a
bind-key C-a last-window
# run nested multiplexers and send prefix with ctrl+a a as in screen
bind-key a send-prefix
# make pane right size
bind-key k run-shell "/path/to/the/script/below.sh"
I use this to set my irssi-pane to the correct width and height when I resize my client. I have a 3-pane setup with one pane to the left and two panes to the right split vertically so I really only need to resize this one pane and the other two will follow along.
$ses is session id. I guess changing it to a sessionname should work, have yet to try it though.
$win is the window in which your pane resides.
$id* is pane id within the window which you can get with ctrl+a q.
$height is the height you want your pane to be.
$width … duh.
You might want to take these variables from stdin, I just couldn’t be bothered.
#!/bin/bash
ses=0
win=1
id=2
height=50
width=80
paneWidths=$(tmux list-panes -t $ses:$win | awk '{print $2}' | cut -c 2- | cut -f 1 -d'x')
paneHeights=$(tmux list-panes -t $ses:$win | awk '{print $2}' | cut -c 2- | cut -f 2 -d'x' | cut -f 1 -d']')
currentWidth=$(echo $paneWidths | awk "{ print \\$($id+1) }")
currentHeight=$(echo $paneHeights | awk "{ print \\$($id+1) }")
resizeNumW=$(expr $currentWidth - $width)
resizeNumH=$(expr $currentHeight - $height)
if [ ${resizeNumW} -ge 0 ]; then
dirw="R"
else
dirw="L"
resizeNumW="$((-${resizeNumW}))"
fi
if [ ${resizeNumH} -ge 0 ]; then
dirh="D"
else
dirh="U"
resizeNumH="$((-${resizeNumH}))"
fi
## DEBUG
#echo ws: $paneWidths
#echo hs: $paneHeights
#echo cw: $currentWidth
#echo ch: $currentHeight
#echo rw: $resizeNumW
#echo rh: $resizeNumH
#echo dw: $dirw
#echo dh: $dirh
if [ ${resizeNumW} -ne 0 ]; then
tmux resize-pane -t $ses:$win.$id -$dirw $resizeNumW
fi
if [ ${resizeNumH} -ne 0 ]; then
tmux resize-pane -t $ses:$win.$id -$dirh $resizeNumH
fi
I might expand on this at a later time… maybe.