How do I start tmux with N equally-sized, vertically-split panes given an arbitrary list of startup directories?

I want a shell script that will launch tmux with N equally-sized, vertically-split panes, each with its own starting directory, where N is determined by the number of starting directories specified. For example,

tmux-launch SESSION-NAME DIR1 DIR2 DIR3 ...

It would be very useful to be able to provide an intial command(s) corresponding to each starting directory. For example,

tmux-launch SESSION-NAME DIR1:"CMD1" DIR2:"CMD2" DIR3:"CMD3" ...

Here is a script I wrote to do this. Save it as (chmod +x


if [[ -z $2 ]]; then
    current=$(tmux display -p "#S")

USAGE: $(basename $0) SESSION DIR [DIR] ...

        [DIR]   one or more startup directories; at least one is required

    if [[ -n $current ]]; then
        echo "current session: $current"
    exit 1

# start tmux
tmux new-session -s $session -d

args=( "$@" )

for e; do

    # capture directory and optional command from $e
    d=$(echo $e | cut -d: -f1)
    c=$(echo $e | cut -d: -f2)

    if [ ! -d $d ]; then
        echo WARNING: directory does not exist, $d >&2

    # Select pane $i, set dir to $d, run pwd
    if [ $i -gt 1 ]; then
        tmux split-window
    tmux select-pane -t $i
    tmux send-keys "cd $d" C-m
    if [ -n "$c" ]; then
        # execute optional command
        tmux send-keys "$c" C-m

    # equally-spaces
    tmux select-layout even-vertical

    i=$(expr $i + 1)


# Finished setup, attach to the tmux session!
tmux attach-session -t $session

For example, the following usage will launch a tmux session named "work" with five panes, each starting in a unique directory, and execute the pwd command in each pane and echo tada command in the first (of course this exmple assumes that the ~/scratch* directories exist): work ~/scratch1:"pwd;echo tada" 
                    missing-directory:"echo do nothing" 
                    ~/scratch2:pwd ~/scratch3:pwd ~/scratch4:pwd ~/scratch5:pwd

This tool will skip any non-existent directory, and print a warning to stderr.

I’ve testing this on Red Hat Enterprise Linux and SUSE Linux Enterprise Server distros using tmux versions 2.7, 3.1c, 3.2a and 3.3.

EDIT 2024-03-02: A more complicated edition of the script described above is found here. This extended script provides some additional options, including horizontal split and tiled layouts.

EDIT 2024-03-04: This script assumes that your tmux configuration begins with pane number 1 instead of the default 0. For example, in my ~/.tmux.conf:

# Set the base-index to 1 rather than 0
set -g base-index 1
set-window-option -g pane-base-index 1
Answered By: David A. Pimentel
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.