5

Let's say I am using a tmux window with 4 panes (like the example in this question. Now I change some environment variable and I want the new value to be recognized in all 4 panes. Is there a simple way to respawn all 4 panes with one command?

I know that :respawn-pane -k works for any one pane, but it's tiresome to have to enter it 4 times. I also tried respawn-window, but that left me with a single new pane (it killed my 4-pane view).

Thanks

John
  • 151
  • 4
  • That's an interesting idea. I seem to recall wondering if there was a tmux solution to propagating an environment variable change across open shells a few years ago, but I didn't find one at the time. Don't recall considering :respawn-pane though, so maybe the solution lies along that path. My solution ended up being a change from zsh to fish (for this and other reasons). fish has "universal variables" which take effect in all open shells when set/changed. – NotTheDr01ds May 13 '21 at 17:40

2 Answers2

2

If nothing else, it seems to be easily scriptable (although please see the comments for warnings on a better approach):

bash/zsh: for pane in $(tmux list-panes -a -F "#D"); do tmux respawn-pane -k -t $pane; done

fish: for pane in (tmux list-panes -a -F "#D"); tmux respawn-pane -k -t $pane; end

As I'm sure you are aware, this should be used with caution since it will end any and all processes that are running in any of the other panes.

NotTheDr01ds
  • 3,547
  • Thanks, that's a good idea. I'm going to leave the question open a little longer (i.e., not accept the answer yet) to see if someone comes up with a way to do it natively in tmux. I appreciate your suggestion. – John May 16 '21 at 18:15
  • 1
    Careful with using a for loop with the output of all. Would recommend including #{pane_active} in your format, then sort on that column. This way the last pane to be closed will be the active one you are on. If you dont do this the for loop will work though every entry in the list provided from the output of tmux list-panes -a -F "#D", and if your script is in the middle it will stop before the rest of the entries are complete. – Dave Aug 04 '22 at 14:28
  • @Dave Great point! I'm probably not going to fix the answer itself right now, but let your comment serve as note to future readers. If a future reader needs assistance with the modification, please feel free to ask in a comment here tagging me or Dave. – NotTheDr01ds Aug 04 '22 at 17:59
0

I'm not sure why I didn't mention this when the question was originally asked, but as an orthogonal answer, the use case in this question is one of the main reason why I switched to the fish shell a few years ago.

Fish has a feature called "universal variables" which get updated/changed in all running shell instances (and persisted). For example:

# Long form
set --universal --export some_sock /run/myapp.sock
# Short form
set -Ux some_sock /run/myapp.sock

All of my tmux panes that are running fish will have the new variable and/or updated value.

NotTheDr01ds
  • 3,547