2

Consider the code :

tmux new-session \; split-window -h \; split-window -v -p 66 \; split-window -v \; split-window -v \; select-pane -t 0 \; split-window -v -p 66 \; split-window -v -p 66 \; split-window -v \;

How can we add to this code the ability to open each pane in a different path (cd ...) and also run a command in each pane : npm run dev

muru
  • 197,895
  • 55
  • 485
  • 740
JAN
  • 145

2 Answers2

7

Try the following code in a script - works for me on Tmux 3.1:

#!/bin/bash

Create a new session named "newsess", split panes and change directory in each

tmux new-session -d -s newsess tmux send-keys -t newsess "cd /path/to/directory1" Enter tmux split-window -h -t newsess tmux send-keys -t newsess "cd /path/to/directory2" Enter tmux split-window -v -p 66 -t newsess tmux send-keys -t newsess "cd /path/to/directory3" Enter tmux split-window -v -t newsess tmux send-keys -t newsess "cd /path/to/directory4" Enter tmux split-window -v -t newsess tmux send-keys -t newsess "cd /path/to/directory5" Enter tmux select-pane -t newsess:0.0 tmux split-window -v -p 66 -t newsess tmux send-keys -t newsess "cd /path/to/directory6" Enter tmux split-window -v -p 66 -t newsess tmux send-keys -t newsess "cd /path/to/directory7" Enter tmux split-window -v -t newsess tmux send-keys -t newsess "cd /path/to/directory8" Enter

Set pane synchronization

tmux set-window-option -t newsess:0 synchronize-panes on

Run command in all 8 panes

tmux send-keys -t newsess "npm run dev" Enter

Attach to session named "newsess"

tmux attach -t newsess

For testing I'd recommend running pwd instead of whatever command you wish to run in 8 panes simultaneously (also to check directories are correct). ;-)

If you want to run different commands in each pane, you of course just run the command with send-keys after changing directory. But I was under the impression that you want to run the same command in each pane, and that's why pane synchronization would work.

Artur Meinild
  • 26,018
  • Thanks , also can we run two commands in tmux send-keys -t newsess "cd /path/to/directory5" Enter ? I want to run cd && cd /path/to/directory{XXX} in the same time – JAN Mar 04 '21 at 11:40
  • 1
    Yes I believe that would work. Please accept the answer when you have tested it works out for you. :-) – Artur Meinild Mar 04 '21 at 11:45
1

Tiny improvement; If you want all panes to be equal size, the script becomes as following:

# Create a new session named "newsess", split panes and change directory in each
tmux new-session -d -s newsess
tmux send-keys -t newsess "cd /path/to/directory1" Enter
tmux split-window -h -t newsess
tmux send-keys -t newsess "cd /path/to/directory2" Enter
tmux split-window -v -p 75 -t newsess
tmux send-keys -t newsess "cd /path/to/directory3" Enter
tmux split-window -v -p 66 -t newsess
tmux send-keys -t newsess "cd /path/to/directory4" Enter
tmux split-window -v -p 50 -t newsess
tmux send-keys -t newsess "cd /path/to/directory5" Enter
tmux select-pane -t newsess:0.0
tmux split-window -v -p 75 -t newsess
tmux send-keys -t newsess "cd /path/to/directory6" Enter
tmux split-window -v -p 66 -t newsess
tmux send-keys -t newsess "cd /path/to/directory7" Enter
tmux split-window -v -p 50 -t newsess
tmux send-keys -t newsess "cd /path/to/directory8" Enter
# Set pane synchronization
tmux set-window-option -t newsess:0 synchronize-panes on
# Run command in all 8 panes
tmux send-keys -t newsess "npm run dev" Enter
# Attach to session named "newsess"
tmux attach -t newsess