12

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
2
  • You may consider to use an editor that fills the paragraph up to the column you specify, 80 in your case. Check emacs.
    – Luis
    May 3, 2013 at 13:58
  • 1
    vim 7.3+ has a colorcolumn setting that adds a column of an alternate background at the Nth character. P.S> I'll never turn to the dark side ;)
    – JKirchartz
    May 3, 2013 at 15:15

4 Answers 4

9

I found the code for the accepted answer useful but, I am using tmux 1.8 and this appears to be a feature built in. From the tmux man pages under split-window: The -l and -p options specify the size of the new pane in lines (for vertical split) or in cells (for horizontal split), or as a percentage, respectively. So, simply using -l insead of -p should have the effect you are looking for. ex:

split-window -h -l 80

A similar solution for resize-pane is described here.

5

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"

7
  • this appears to work perfectly, is there a way to run it automatically when I load my workspace? (also I've updated my question with a little more info)
    – JKirchartz
    May 3, 2013 at 15:53
  • @JKirchartz: Updated with source command. (Nice to see people using decent tools btw. ;)) Have been using screen and only recently have started to try to force myself over to tmux.
    – Runium
    May 3, 2013 at 17:09
  • Yeah, I was trying to figure out vim splits, and I wasn't very happy with the workflow, checked out screen for a day before finding tmux, I love it. Set it up to be vim-like and now it's a breeze.
    – JKirchartz
    May 3, 2013 at 17:12
  • just noticed a bug, you're not defining $pane anywhere, replacing that with 0 worked perfectly, thanks!
    – JKirchartz
    May 3, 2013 at 18:32
  • @JKirchartz: Ah, thanks. I copied it from some code where $pane was optional argument 2 and where I tried to determine where pane was (left/center/right) and use "R" or "L" accordingly, but proved to be hard to detect. Corrected.
    – Runium
    May 3, 2013 at 18:38
0

An easy way to do this is to use the main-vertical layout with C-b M-4 or C-b : selectl main-vertical. It's designed for emacs so the left pane is 81 columns, you'll need to change the main-pane-width option to make it 80.

0

Things have changed since the question and last answer was posted. tmux has hooks which can be used to keep the width of main pane fixed.

for example I use following alias:

alias work-bftp="tmux attach -t work-bftp || tmux new -s work-bftp -d 'bash' \; set -g default-command bash \; split-window -h \; set-option -t work-bftp main-pane-width 80 \; selectl main-vertical \; set-hook -t work-bftp window-layout-changed 'selectl main-vertical;' \; attach;"

to create a main-vertical layout based window, where main pane is used for 80 column wide vim usage in first window. This uses window-layout-changed event to redraw window with desired size.

I tried, and it worked for a while, with client-resized event too, but it stopped working one day for terminator/ emulator window maximize operations.

one problem with this approach though is, prefix-z does not work anymore for the first window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .