0

Scenario: I edit ~/.zshrc, and I want to source it in all of my tmux panes, in all windows, in a given session. I want to do this with a keybinding, from any pane.

I tried looking at How to send a command to all panes in tmux?, which used setw synchronize-panes on and then ran a tmux ex mode command; but I'd like to run source $HOME/.zshrc, (a shell command), and I'm a little confused as to how to do that.

I'd like it to be a keybinding that I can put into .tmux.conf, so that I only have to press prefix + <key_combo> to perform this, and then everything returns to the way it was before I ran this (e.g. I don't have to worry about unsetting any tmux options that the command set)

pynexj
  • 19,215
  • 5
  • 38
  • 56
Life5ign
  • 192
  • 11

1 Answers1

2

This would work for you:

# send "source ~/.bashrc<Enter>" to all panes in *current* session
bind  B  run 'panes=`tmux list-panes -s -F "##{pane_id}"`; \
              for pane in $panes; do \
                tmux send -t $pane "source ~/.bashrc" Enter; \
              done'

# send "source ~/.zshrc<Enter>" to all panes in *current* session
bind  Z  run 'panes=`tmux list-panes -s -F "##{pane_id}"`; \
              for pane in $panes; do \
                tmux send -t $pane "source ~/.zshrc" Enter; \
              done'
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • thanks for the reply; this worked great, except in panes where another process, e.g. `vim`, is running (for vim panes it ran the "s" command (substitute 1 character), and then entered `ource ~/.zshrc`, and stayed in insert mode, as expected). vim is usually the only longstanding process I have running in panes in a session; is there a way to check that the foreground process in a pane is zsh, or not vim (for a narrower case), and only run this command in those panes? – Life5ign Jan 29 '23 at 19:33
  • 1
    that's not easy to be answered in the comment. please ask a new question. – pynexj Jan 30 '23 at 01:16