Make a tmux pane 80 (or so) columns wide

tmux

I'm trying to follow the 80 column rule when writing my code, my current tmux setup is split 50/50 horizontally. I like to have vim in the left-hand pane, and in the right-hand pane I have a 75/25 split where I run other things.

On my side monitor 50% is 76 columns wide, but on my laptop's display it's 123 column wide.

I'd like to maximize real-estate for other commands, Is there a way to set this to exactly 80 columns (or so) when I launch my workspace?

I'm currently setting up my workspace with:

bind C-w source-file ~/dotfiles/scripts/tmux_work_layout

that file contains:

selectp -t 0              # Select pane 0
splitw -h -p 50           # Split pane 0 vertically by 50%
selectp -t 1              # Select pane 1
splitw -v -p 25           # Split pane 1 horizontally by 25%
selectp -t 0              # Select pane 0

Best Answer

Is it something like this you want?

Add to file and make executable by chmod +x filename. call by e.g.

./sizetmux       # Default hardcoded size
./sizetmux 85    # Specify width

To run it from sourced file:

if-shell /path/to/script/sizetmux 80

Code:

#!/bin/bash

# Wanted pane width 0 - default 80, or pass argument 1 as wanted width
pw0=80
[[ "$1" =~ ^[0-9]+$ ]] && pw0="$1"

# This could be done nicer, but, anyhow: getting current width of pane 0
pw0_cur_w=$(tmux list-panes | awk -F"[ x:\\\[\\\]]+" '/^0:/{print $2}')

# Resize according to current width
if [[ "$pw0_cur_w" -eq "$pw0" ]]; then
    echo "OK $pw0"
elif [[ "$pw0_cur_w" -gt "$pw0" ]]; then
    ((w = pw0_cur_w - pw0))
    echo "$w less"
    tmux resize-pane -L -t 0 "$w"
elif [[ "$pw0_cur_w" -lt "$pw0" ]]; then
    ((w = pw0 - pw0_cur_w))
    echo "$w more"
    tmux resize-pane -R -t 0 "$w"
fi

One also have to take into account e.g. line-numbers in vim so perhaps 85?


Edit perhaps a bit nicer (not so much clutter) (after pw0_cur_w=$(tm ...

((w = pw0_cur_w - pw0))

if [[ "$w" -ge 0 ]]; then
    dir="L"
    echo "$w less"
else
    dir="R"
    ((w *= -1))
    echo "$w more"
fi

[[ "$w" -gt "0" ]] && tmux resize-pane -"$dir" -t 0 "$w"

Related Question