Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use bash script to setup tmux windows

Tags:

bash

tmux

I'm trying to write a script that will setup tmux windows for rails development, so far I have this:

#!/bin/bash
tmux new-window -n vim
tmux new-window -n guard
tmux new-window -n console/server

tmux select-window -t 3
tmux send-keys -t 3 'rails server' C-z

What I want, on that specific window, is to run rails server and then send it to the background. The thing is, all I get is ^Zand the usual prompt after a couple of seconds.

How can I accomplish this?

like image 739
Fábio Queluci Avatar asked Sep 10 '15 12:09

Fábio Queluci


People also ask

How do I add tmux to Windows?

You can also access a tmux command line and type tmux commands by name. For example, to create a new window the hard way, you can press Ctrl+B followed by : to enter the tmux command line. Type new-window and press Enter to create a new window. This does exactly the same thing as pressing Ctrl+B then C.

How can I set my default shell to start up tmux?

Configure Terminal to start tmux by defaultbash_profile shell startup file, just above your aliases section. Save the file and close it. Then close and reopen the terminal to start using tmux by default, every time you open a terminal window.

How do I start a tmux process?

Using the Prefixes to Control Tmux By default, the prefix is CTRL+B. That is, we have to press the keys CTRL+B and then the command. For example, to create a new session, the command would be C. So, to create a new session we need to press CTRL+B and next C – CTRL+B, C.


1 Answers

You can combine those tmux commands so that they’re all received by the same session. Separate by \; (escaped to get all the way to tmux):

#!/bin/bash

tmux new -stest \; \
  new-window -n vim \; \
  new-window -n guard \; \
  new-window -n console/server
  select-window -t 3 \; \
  send-keys -t 3 'rails s &' Enter

I don't see the C-z working, but you can achieve the same with & backgrounding.

like image 162
Micah Elliott Avatar answered Nov 10 '22 11:11

Micah Elliott