Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So, I want to make a project-specific tmux session that pops up certain commands in certain panes automagically. Following an online tutorial and the tmux man page, this is what I come up with for a configuration file:

new -n estruct ls
neww -n estruct2 ls
splitw -v -p 50 -t 0 ls

The result is ... nothing. tmux starts up with a single window and single pane in that window with no commands executed.

How would I go about making a configuration file that actually allows me to create a set of windows and panes with certain commands pre-executed?

share|improve this question

4 Answers 4

up vote 24 down vote accepted

Your configuration file is working, but there are a couple of complications.

Short-lived Commands

First, the commands you are starting (instances of ls) finish running very quickly. The windows and panes for the commands are created, but they disappear as soon as each command exits.

If your goal is to actually use such “one shot” commands in your eventual configuration, then you should probably use the window option remain-on-exit (and possibly the session option set-remain-on-exit to provide a default for all windows created in the session). When remain-on-exit is set for a window, it will not disappear when the command exits. You will probably want to map the respawn-window to a key (note: respawn-window will respawn the original command; respawn-pane is also available in tmux 1.5 to respawn individual panes).

bind-key R respawn-window

# set global r-o-e so that initial window of initial session gets it
set -g set-remain-on-exit on

# create a session and its windows/panes
new -n estruct ls
neww -n estruct2 ls
splitw -v -p 50 -t 0 ls

# restore global r-o-e
set -g set-remain-on-exit off

# let session r-o-e inherit from global value for future windows
set -u set-remain-on-exit

Or, if you do not mind your initial window being number 1 instead of number 0, then we can avoid changing the global value:

bind-key R respawn-window

# create a session with a throw-away window
new true

# for future windows, stay open after the command exits
set set-remain-on-exit on

# create the windows we really want
neww -n estruct ls
neww -n estruct2 ls
splitw -v -p 50 -t 0 ls

# for future windows, revert r-o-e to global value
set -u set-remain-on-exit

If you were only using ls as a simplified placeholder command and you actually intend to spawn some type of interactive command in your windows/panes, then you should probably just spawn the command you intended to eventually run. If your intended command takes too long to startup to use it during tmux testing, then substitute a shell or other simple interactive command (e.g. top, sqlite3, etc.).

new-session Is the Default Command

Second, running tmux without a command argument (e.g. tmux) is equivalent to using the new-session command (i.e. tmux is the same as tmux new-session). Your .tmux.conf creates a session for its windows/panes (new is an alias for new-session) and another session is being created for the implicit new-session command specified by starting tmux without a command argument. If you are using the default status bar, you will see a [1] in the left side of the status bar when you are in the “extra” session (the initial session created in the .tmux.conf is number 0).

If you want to avoid creating the extra session, then use tmux attach instead of plain tmux. The commands from your .tmux.conf will run, creating session 0, and then your client will attach to that session instead of creating a new one.

share|improve this answer
2  
OK, I'm maybe being as thick as a whale sandwich here, but right now I've got a configuration file that has only splitw in it -- nothing else -- and I'm still not getting a split screen because it says it can't establish the current session. If I add new before it, I get ... one window, one pane. I can't seem, for the life of me to get two panes at start-up. –  JUST MY correct OPINION Oct 18 '10 at 2:21
    
A config file with new, then splitw works for me when I start tmux via tmux attach. One thing to remember is that the .tmux.conf (or the file specified with the -f option) is only used if there is no server already running. Check whether a server is running via tmux ls. If you want to run the commands from a file when a server is already running, then do it like this: tmux source-file /path/to/conf-file-that-creates-a-session-and-windows \; attach (if you run this when there is no server your .tmux.conf (or the -f file) will run first then your source-file file). –  Chris Johnsen Oct 18 '10 at 3:09
    
The config file is being specified with -f -- so I'm going tmux new -f myconfig. The contents of that config file contains only new followed by splitw. I am killing any and all tmux processes before I try the command line tmux -f myconfig. I don't get two panes. I'm really at a loss here with what to do past this point (aside from just giving up on tmux). –  JUST MY correct OPINION Oct 18 '10 at 7:23
4  
@Just: Whether you use -f or not, tmux without a command argument is the same as tmux new-session (which, for new servers, will create a new session on top of whatever your config file does). Your pre-split window is created in session 0, but the implicit new-session command is creating (and attaching to) an extra session (session 1; look for the [1] in your status line). Use tmux -f myconfig attach to avoid creating the new session that is hiding your pre-split window. –  Chris Johnsen Oct 18 '10 at 9:03
1  
OK, I guess learning to read will eventually pay off for me. :) Thanks for the effort and the answer. Everything's working fine now. I wish there was a reputation "tipping jar" here so I could give you more than just an accepted answer. –  JUST MY correct OPINION Oct 18 '10 at 9:59

I'm not sure I understand what you mean by "manually entering", but this is how I do it. I wrote an "autostart" script that my window manager launches. It contains:

tmux start-server                                                                                                                                                                                                                                                                                                                                                            
for i in ~/sessions_tmux/*.session; do "$i"; done

where each .session file looks something like this:

#!/bin/bash

SESSION_NAME="flaskr_tutorial"
if [[ ! -z "$TMUX" ]] || [[ ! -z "$TMUX_PANE" ]]; then
    echo "Already inside a tmux session, do not know what to do"
    exit 1
fi
tmux -q has-session -t "$SESSION_NAME"
if [ $? ]; then
    exit 0
fi
NEWCWD=~/projects/flaskr/
( cd "${NEWCWD}"; tmux new-session -d -s "$SESSION_NAME" )
tmux send-keys -t "$SESSION_NAME" 'vim flaskr.py' Enter
( cd "${NEWCWD}"; tmux split -h -t "$SESSION_NAME" )
tmux send-keys -t "$SESSION_NAME" 'sudo tail -F -n 100 /var/log/messages | ccze' Enter

Wait, there's more! I have an ever growing number of .session files that I load up with konsole:

konsole --background-mode --profile Shell --tabs-from-file ~/sessions_tmux/work_console.tabs

where work_console.tabs looks something like this:

.
.
.
title: projectx;;    command: tmux attach -t projectx
title: flaskr;;      command: tmux attach -t flaskr_tutorial
.
.
.

Then I press F12 to launch konsole.

share|improve this answer
    
konsole seems to be broken so I've switched to Terminal, which works well. I have a more detailed post on the tmux-users mailing list. –  John Schmitt Oct 1 '12 at 0:11

I found teamocil very useful for automating tmux windows/sessions.

Its written in Ruby so you need that installed.

Here is a sample configuration:

session:
  name: 'my project'
  windows:
    -name: vim
     root: '~/code/my_project'
     splits:
       - cmd: vim
       - cmd: guard

You can have an arrangement of windows and splits.

share|improve this answer

Another alternative similar to teamcoil is tmuxinator, it's on active development and works quite well. It's also recommended (and kind of reviewed) on the amazing tmux: Productive Mouse-Free Development from The Pragmatic Bookshelf.

share|improve this answer
    
Found it a tad bit cleaner than teamocil (but very similar nonetheless) and am using it right now. I'm pretty happy with how it works! –  Aktau Aug 12 '13 at 11:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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