Search code examples
terminaltmux

Loading tmux sessions from file


I'd like to be able in tmux to load sessions saved in a file including window/pane and shell setups. I have this in ~/.tmux/my_session:

new -d -s test1
neww -k -t test1:1 top

new -d -s test2
neww -k -t test2:1 -n '2w1' 
neww -t test2 -n '2w2' 

Now I source it in tmux and this works fine: I have two detached sessions test1 (with one window) and test2 with 2 windows. But I'd like to actually run also some commands, e.g. replace the last line with:

neww -t test2 -n '2w2' 'cd ~/work; ./myscript.zsh ; ls'

So that it cds to the directory, runs some setup scrip, does ls and stays open for further use. According to the manual however windows close automatically after command finishes. There is an option remain-on-exit but it's not clear how could I combine it it with neww so that it actually works...

Also the manual list a -c flag for neww to set starting dir, but my homebrew installed tmux v1.6 doesn't seem to recognize it.

teamocil seems to be able to do it so it has to be possible (but teamocil doesn't keep the sessions separate which is essential for me).

Thank you!


Solution

  • The answer is send-keys:

    new -d -s test1
    neww -k -t test1:1 -n '1w1'
    send-keys -t test1:1 "echo '1w1'" C-m
    
    new -d -s test2
    neww -k -t test2:1 -n '2w1'
    neww -t test2 -n '2w2'
    send-keys -t test2:1 "echo '2w1'" C-m
    send-keys -t test2:2 "echo '2w2'" C-m
    
    display-message "Sessions loaded!"