How to start tmux with several windows in different directories?

Michael Härtl picture Michael Härtl · Dec 16, 2011 · Viewed 46.1k times · Source

I want to use a script to open a tmux session with 6 windows, each in a different directory. I started with a script I found and tried this first:

tmux new-session -s xyz   -n etc -d 'cd /etc'
tmux new-window  -t xyz:1 -n var    'cd /var/log'

But I soon found out that this will not work as I expected — the window will be closed after the shell command completes.

So my next idea was to start a new shell like this:

tmux new-session -s xyz   -n etc -d 'cd /etc; bash -i'
tmux new-window  -t xyz:1 -n var    'cd /var/log; bash -i'
tmux new-window  -t xyz:2 -n var2   'cd /var/log; bash -i'
tmux new-window  -t xyz:3 -n var3   'cd /var/log; bash -i'
tmux new-window  -t xyz:4 -n var4   'cd /var/log; bash -i'
tmux new-window  -t xyz:5 -n var5   'cd /var/log; bash -i'
tmux new-window  -t xyz:6 -n var6   'cd /var/log; bash -i'

tmux select-window -t xyz:1
tmux -2 attach-session -t xyz

This almost works. But if I start more than about 4 windows, I frequently see the following bash errors in one of the windows after startup:

bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected

I have no idea why this happens, but I still think that I’m not doing this right. Is there a better way to set up a tmux session over several directories?

Answer

Will picture Will · Mar 29, 2013

Tmuxinator is also really good for this. Basically you create setup files like so:

# ~/.tmuxinator/project_name.yml
# you can make as many tabs as you wish...

project_name: Tmuxinator
project_root: ~/code/rails_project
socket_name: foo # Not needed. Remove to use default socket
rvm: 1.9.2@rails_project
pre: sudo /etc/rc.d/mysqld start
tabs:
  - editor:
      layout: main-vertical
      panes:
        - vim
        - #empty, will just run plain bash
        - top
  - shell: git pull
  - database: rails db
  - server: rails s
  - logs: tail -f logs/development.log
  - console: rails c
  - capistrano:
  - server: ssh me@myhost

Then you can start a new session with:

mux project_name

I've been using it for a while and have had a good experience for the most part.