48

In my tmux.conf file I have it configured to open windows, name them, setup panes etc.. etc..

However one issue I have is that if one of the panes launches a command, let's say ls, then the pane closes itself after the command completes (instantly). Is there any way to prevent this behavior? Or have it drop to a normal shell after a command completes?

I am assuming that I need to start a shell -> execute command when the pane launches, but I'll be damned if I can figure out how. I have googled a bit for this problem but have come up short.

Caleb
  • 70,105
Stew
  • 481

5 Answers5

41

You have a couple of options.

  1. Instead of running ls in your window, run a shell, then send the shell keystrokes to execute:

    tmux start-server  
    tmux new-session -d -s session  
    tmux new-window -t session:1  
    tmux send-keys -t session:1 ls C-m
    
  2. You can lunch a sequence of commands in such a way as to leave yourself with a bash shell after your other commands have run:

    tmux start-server  
    tmux new-session -d
    tmux new-window 'ls;bash -i'
    
  3. See jasonwryan's answer for details on the remain-on-exit option to keep panes alive after their process has exited so you can review the output.

  4. If the output of some command was worth seeing once, it might be worth refreshing. If you are monitoring the output of something you can watch to periodically get new output. This should play nicely with panes in tmux:

    tmux start-server  
    tmux new-session -d
    tmux new-window 'watch -n 60 ls'
    
Caleb
  • 70,105
  • Thanks man! Awesome answer, number 2 and number 4 are exactly what I was working for, but number 1 seems like a great alternative as well.

    Thanks for all the help, it's really appreciated (both of you).

    – Stew Jul 21 '11 at 12:02
  • 2
    The advantage of #1 is that the command remains in the shell history and can be easily re-executed. If the command was big and ugly, then this is very helpful. – goertzenator Sep 17 '13 at 20:38
  • 1
    It is working for me without the start-server command, I think it is started automatically if needed. Is there an advantage of calling it explicitly nonetheless? – luator Nov 13 '17 at 14:30
  • 1
    @luator I think auto-starting a server if none is found might have been added to tmux since I wrote this answer. I don't know of any advantage if your version spawn a server process if necessary. – Caleb Nov 13 '17 at 14:57
22

You could use the remain-on-exit option:

remain-on-exit [on | off]
A window with this flag set is not destroyed when the program running in it exits. The window may be reactivated with the respawn-window command.

To simplify the respawning process, you might want to bind it to a key:

bind-key R respawn-window

This will ensure you aren't left with dead windows when the programs exit.

Ben
  • 103
jasonwryan
  • 73,126
1

Here is an extended example using the send-keys method recommended by @Caleb

#!/bin/sh
tmux start-server
tmux new-session -d -n 'mywindowname'
tmux send-keys -t mywindowname 'cd notes' Enter 'vim whiteboard/tasks.md' Enter
tmux new-window -d -n 'secondwindowname' # this -d prevents focus from changing to the new window
tmux -2 attach-session -d
jtpereyda
  • 421
  • 1
  • 7
  • 18
1

If you want to customize auto-closing behavior when creating a window with a single command, tmux new-window ';' set -w remain-on-exit <on/off> may be what you are looking for

0

maybe it's enough to add && bash after your command

My Example:

tmux new-session -d 'ls && echo && bash' \; split-window -d 'vi' \; attach
ist
  • 1