17

How can I write the following in a bash script?

tmux # Start tmux session.
compass watch /path/to/project1/compass/ # Run the first process.
Ctrl + B, " # Split the pane.
compass watch /path/to/project2/compass/ # Run the second process.
Ctrl + B, D # Exit the session.
19
tmux \
    new-session  'compass watch /path/to/project1/compass/' \; \
    split-window 'compass watch /path/to/project2/compass/' \; \
    detach-client

The new-session command (which creates a new tmux session) and the split-window command (which splits the current window into two panes) in tmux takes optional shell commands to run. The detach-client does the obvious at the end.

If you want a horizontal split (two panes side by side), use split-window -h in the command above.

When sending multiple tmux commands to tmux you need to separate them by ;. The ; needs to be protected from the shell by quoting/escaping it (';', ";" or \;), to stop the shell from interpreting it as the end of the tmux command.

I've split the whole thing into separate lines for readability. If you do this in a script (which I recommend), make sure there's nothing after the final \ on each line.

Reattach to the session with tmux a, tmux attach, or tmux attach-session (these are all equivalent).

The tmux session will end once both commands have finished executing.

2

This didn't work for me ( I was trying to do something like 'ls -la'). What did was:

tmux new-session -d bash
tmux split-window -h bash
#sends keys to first and second terminals
tmux send -t 0:0.0 "<my-first-command>" C-m
tmux send -t 0:0.1 "<my-second-command>" C-m
tmux -2 attach-session -d

This allowed me to run pretty generic stuff and although it looks ugly, it is pretty functional.

Just leaving it here in case other people are looking for the same thing.

Source: https://gist.github.com/kizzx2/4739236

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.