Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tmux: programatically split a window horizontally and run two commands?

Tags:

linux

macos

tmux

(dupe note) This is not a dupe of How to set up tmux so that it starts up with specified windows opened?. That question revolves around configuring tmux, and none of the answers there provide an answer to this question. (end note)

Suppose I have two commands

tail -f log1
tail -f log2

How can I programatically call tmux to split a window horizontally and run each of the commands in its own pane, something similar to:

for i in log1 log2; do xterm -e tail -f $i& done
like image 916
Mark Harrison Avatar asked Mar 02 '16 17:03

Mark Harrison


1 Answers

There's no single command to accomplish this; rather, you send multiple commands to the server. This can be done, though, via a single invocation of tmux.

tmux new-session -d tail -f log1 \; split-window tail -f log2 \; attach

Note that an escaped semicolon is used to separate tmux commands. Unescaped semicolons are treated as part of the shell command executed by a particular tmux command.

Adapting the loop in your question might look something like:

tmux_command="new-session -d"
commands=("tail -f log1" "tail -f log2" "tail -f log3")
tmux_command+=" ${commands[0]}"
for cmd in "${commands[@]:1}"; do
    tmux_command+="\; split-window $cmd"
done
tmux "$tmux_command \; attach"
like image 138
chepner Avatar answered Oct 04 '22 18:10

chepner