6

(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
Community
  • 1
  • 1
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • 1
    Possible duplicate of [How to set up tmux so that it starts up with specified windows opened?](http://stackoverflow.com/questions/5609192/how-to-set-up-tmux-so-that-it-starts-up-with-specified-windows-opened) – hek2mgl Mar 02 '16 at 17:49
  • Not a dupe. I'm not interested in configuring tmux startup, just how to split the current window as described and run the commands. None of the questions answer that. – Mark Harrison Mar 02 '16 at 17:56
  • Note that I'm looking for a single call that will do this. Your comment involves manually typing two commands. – Mark Harrison Mar 02 '16 at 19:17
  • **What** do you want? I gave you a link to a single command, and explained commands which can be manually typed when inside a session. Both of them fit your use case. – hek2mgl Mar 02 '16 at 20:15
  • I think I'm a bit dumb... I'm not seeing the single command you're linking to. Try providing the answer as an answer, see how I updated to show the xterm example. It would be most appreciated, as my dumbness is preventing me from understanding what you're saying. – Mark Harrison Mar 02 '16 at 22:25
  • Oh, and keep in mind that I would like to do this programatically. If I were simply asking how to use tmux to split a screen, it would be better asked on one of the other SO sites. – Mark Harrison Mar 02 '16 at 22:29

1 Answers1

6

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"
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
chepner
  • 497,756
  • 71
  • 530
  • 681