ysiripragada
ysiripragada

Reputation: 151

tmux split-window using shell script

I am trying to write shell script for creating panes in tmux.

#!/bin/sh
tmux rename-window user
tmux new-session -d
tmux split-window -h
tmux selectp -t 1
tmux split-window -h
tmux select-layout even-horizontal
tmux selectp -t 2
tmux split-window -v

But when executing the above code doesn't produce desired output.

The picture indicates output produced when the code is executed:

Resulting output

Instead when all the commands are written in tmux it produces the desired output.

The picture indicates output produced when commands are manually typed:

Desired output

How can the code be modified to produce the desired outcome?

Upvotes: 15

Views: 14665

Answers (3)

chepner
chepner

Reputation: 530853

You can execute all the tmux commands in a single invocation:

tmux new-session -d \; split-window -h \; split-window -h\; split-window -v

Upvotes: 0

ARC
ARC

Reputation: 21

#!/bin/bash

ssh_list=( `cat $1` )
split_list=()
round=0
i=0
for ssh_entry in "${ssh_list[@]:1}"; do
        if (( $((round%2)) == 0 ))
        then
                split_list+=( split-window -v -t $((2*i)) -p 50 ssh "$ssh_entry" ';' )
        else
                split_list+=( split-window -h -t $((2*i)) -p 50 ssh "$ssh_entry" ';' )
        fi
        mxrd=$((2**round))
        i=$((i+1))
        if ((round==0)) || ((i>=mxrd))
        then
                i=0
                round=$((round+1))
        fi
done

tmux new-session ssh "${ssh_list[0]}" ';' \
    select-layout tiled ';' \
    "${split_list[@]}" \
    set-option -w synchronize-panes

Upvotes: -1

atomicstack
atomicstack

Reputation: 809

Try this, it looks like it does what you need (judging by the "Desired Output" image):

tmux split-window -h
tmux split-window -h
tmux select-layout even-horizontal
tmux split-window -v
tmux select-pane -t 0

You can also try persisting your layout using something like https://github.com/tmux-plugins/tmux-resurrect.

Upvotes: 7

Related Questions