32

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?

6 Answers 6

37

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.

8
  • 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. Commented Oct 18, 2010 at 2:21
  • 1
    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). Commented Oct 18, 2010 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). Commented Oct 18, 2010 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. Commented Oct 18, 2010 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. Commented Oct 18, 2010 at 9:59
10

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.

2
  • 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. Commented Oct 1, 2012 at 0:11
  • 1
    Just a small comment, you can replace '! -z' with '-n' in your tests ;)
    – lilorox
    Commented Nov 28, 2017 at 9:12
8

suppose you want to launch 3 server monitoring tasks (here ping localhost) there's no need to mess with configuration files and saved sessions. It can all be done from the command-line.

tmux new-session 'ping  127.0.0.1' \; \
    split-window 'ping  127.0.0.1' \; \
    split-window 'ping  127.0.0.1' \; \
    select-layout even-vertical
1
  • Thanks for the simplicity - exactly the example I was looking for!
    – dsz
    Commented Sep 17, 2015 at 0:03
5

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.

4

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.

1
  • 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
    Commented Aug 12, 2013 at 11:39
4

**

  • Better Save your Session !

** I found that for a lazy admin like myself, I would rather save the session of tmux, rather than spending hours trying to build it perfectly. I am administering more than 30 servers, and my tmux sessions are dynamic, with configiuration changing all the time. It was pain in the neck, till I recently discovered tmux-resurrect this plugin is just great. Give it a try. Currently, Ubuntu repositories have tmux 1.8, this plugin needs 1.9, so you would need to upgrade manually

sudo apt-get install python-software-properties software-properties-common
sudo add-apt-repository ppa:pi-rho/dev
sudo apt-get update
sudo apt-get install  tmux=2.0-1~ppa1~t

After which you can install it by

git clone https://github.com/tmux-plugins/tmux-resurrect ~/clone/path

Then dd this line to the bottom of ~/.tmux.conf (if ~/.tmux.conf is not available, create an empty file)

run-shell ~/clone/path/resurrect.tmux

I also add this line at the end of the ~/.tmux.conf so that the plugin saves sessions for all programs I run

set -g @resurrect-processes ':all:'

Then reload tmux environment.

tmux source-file ~/.tmux.conf

That's it! You are done. Now, run tmux as normal, resize your panes, create new panes, create new sessions...etc. When you are happy with the configuration you reached ( in several windows and several sessions), just click ctrl+b then ctrl+s to save the session. In order to test the results, just kill all tmux

killall tmux

Then, run again tmux

tmux

Then, from within tmux click ctrl+b then ctrl+r (for restore) Magically, all your windows, panes, and sessions are restored with the same geometry and arrangement. Hope that this would help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .