<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><generator uri="https://gohugo.io/" version="0.101.0">Hugo</generator><title type="html">window on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/tags/window/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/tags/window/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/tags/window/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/tags/window/jf2feed.json" rel="alternate" type="application/jf2feed+json" title="jf2feed"/><updated>2026-04-22T08:24:58-04:00</updated><author><name>Kaushal Modi</name><email>kaushal.modi@gmail.com</email></author><id>https://scripter.co/tags/window/</id><entry><title type="html">Send a command to every pane/window/session in tmux</title><link href="https://scripter.co/command-to-every-pane-window-session-in-tmux/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://scripter.co/command-to-every-pane-window-session-in-tmux/</id><author><name>Kaushal Modi</name></author><published>2014-03-06T09:50:21-05:00</published><updated>2018-03-20T00:00:00-04:00</updated><content type="html"><![CDATA[<blockquote>Faster way to send the same command to each and every <em>pane</em> in your
tmux <em>session</em>.</blockquote><div class="ox-hugo-toc toc">
<div class="heading">Table of Contents</div>
<ul>
<li><a href="#send-command-to-all-panes-in-all-sessions">Send command to all panes in <strong>all</strong> sessions</a>
<ul>
<li><a href="#usage">Usage</a></li>
<li><a href="#about-the-double-hashes">About the <code>##</code></a></li>
</ul>
</li>
<li><a href="#send-command-to-all-panes-in-current-session">Send command to all panes in <strong>current</strong> session</a></li>
<li><a href="#tmux-send-cmd-to-all-panes-old">Older version (circa 2014)</a></li>
</ul>
</div>
<!--endtoc-->
<p>Ever wondered how you would send the <code>clear</code> command to <em>each pane</em>,
in <em>each window</em>, in <em>each session</em> in <code>tmux</code>, or how you would do
source your shell config file in each after each tweak?</p>
<p>Here are few excerpts from my <a href="https://github.com/kaushalmodi/dotfiles/blob/master/tmux/dot-tmux.conf"><code>.tmux.conf</code></a> that allow doing just
that.</p>

<h2 id="send-command-to-all-panes-in-all-sessions">Send command to all panes in <strong>all</strong> sessions&nbsp;<a class="headline-hash no-text-decoration" href="#send-command-to-all-panes-in-all-sessions">#</a></h2>


<p>Thanks to the tip in comments from <em>Bob Fleming</em>, I learned that <code>tmux</code> has a <code>-a</code>
switch for the <code>list-panes</code> command.</p>
<p>From <code>man tmux</code>,</p>
<blockquote>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">list-panes [-as] [-F format] [-t target]
</span></span><span class="line"><span class="cl">              (alias: lsp)
</span></span><span class="line"><span class="cl">        If -a is given, target is ignored and all panes on the server
</span></span><span class="line"><span class="cl">        are listed.  If -s is given, target is a session (or the
</span></span><span class="line"><span class="cl">        current session).  If neither is given, target is a window (or
</span></span><span class="line"><span class="cl">        the current window).  For the meaning of the -F flag, see the
</span></span><span class="line"><span class="cl">        FORMATS section.
</span></span></code></pre></div></blockquote>
<p>With that knowledge, the <a href="#tmux-send-cmd-to-all-panes-old">older version</a> of the <kbd>E</kbd> binding now reduces
to,</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-docker" data-lang="docker"><span class="line"><span class="cl"><span class="c"># Send the same command to all panes/windows/sessions</span><span class="err">
</span></span></span><span class="line"><span class="cl"><span class="err"></span><span class="nb">bind</span> E command-prompt -p <span class="s2">&#34;Command:&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>       <span class="s2">&#34;run \&#34;tmux list-panes -a -F &#39;##{session_name}:##{window_index}.##{pane_index}&#39; \
</span></span></span><span class="line"><span class="cl"><span class="s2">              | xargs -I PANE tmux send-keys -t PANE &#39;%1&#39; Enter\&#34;&#34;</span><span class="err">
</span></span></span></code></pre></div>
<h3 id="usage">Usage&nbsp;<a class="headline-hash no-text-decoration" href="#usage">#</a></h3>


<ul>
<li>Type the following binding in any <code>tmux</code> pane: <kbd>C-z E</kbd><sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup></li>
<li>Enter a command that you would want to send to all the panes, like
<code>source ~/.alias; clear</code> <em>(this is entered in the tmux command
prompt)</em>.</li>
<li>That will source the <code>~/.alias</code> in <strong>all</strong> panes, and then clear the
terminals as well.</li>
</ul>

<h3 id="about-the-double-hashes">About the <code>##</code>&nbsp;<a class="headline-hash no-text-decoration" href="#about-the-double-hashes">#</a></h3>


<div class="note">
<p>The <code>#</code> character needs to be escaped by another <code>#</code> and typed as
<code>##</code>, only when used inside the <code>run-shell</code> command.</p>
</div>
<p>.. because otherwise, <code>tmux run-shell</code> command will replace the
unescaped <code>#{session_name}</code>, <code>#{window_index}</code> and <code>#{pane_index}</code> with
their current values <strong>before</strong> executing the command.</p>
<p>With the hashes escaped, those variables will be evaluated <em>at run
time</em>.</p>
<p>But if you were to type the above command directly in the terminal,
without the <code>run-shell</code> command wrapper, you would use only single
<code>#</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">tmux list-panes -s -F &#34;#{session_name}:#{window_index}.#{pane_index}&#34;
</span></span></code></pre></div>
<h2 id="send-command-to-all-panes-in-current-session">Send command to all panes in <strong>current</strong> session&nbsp;<a class="headline-hash no-text-decoration" href="#send-command-to-all-panes-in-current-session">#</a></h2>


<p>The <code>list-panes</code> command has another useful switch: <code>-s</code>, which takes
an optional argument, a <em>session name</em>. If that argument is not
supplied, it takes the current session name by default.</p>
<p>Below <kbd>C-e</kbd> binding is used to send a command to all panes, in all
windows, but <strong>only in the current session</strong>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-docker" data-lang="docker"><span class="line"><span class="cl"><span class="nb">bind</span> C-e command-prompt -p <span class="s2">&#34;Command:&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>         <span class="s2">&#34;run \&#34;tmux list-panes -s -F &#39;##{session_name}:##{window_index}.##{pane_index}&#39; \
</span></span></span><span class="line"><span class="cl"><span class="s2">                | xargs -I PANE tmux send-keys -t PANE &#39;%1&#39; Enter\&#34;&#34;</span><span class="err">
</span></span></span></code></pre></div>
<h2 id="tmux-send-cmd-to-all-panes-old">Older version (circa 2014)&nbsp;<a class="headline-hash no-text-decoration" href="#tmux-send-cmd-to-all-panes-old">#</a></h2>


<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-docker" data-lang="docker"><span class="line"><span class="cl"><span class="c"># Send the same command to all panes/windows/sessions</span><span class="err">
</span></span></span><span class="line"><span class="cl"><span class="err"></span><span class="nb">bind</span> E command-prompt -p <span class="s2">&#34;Command:&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>       <span class="s2">&#34;run \&#34;tmux list-sessions                                           -F &#39;##{session_name}&#39; \
</span></span></span><span class="line"><span class="cl"><span class="s2">              | xargs -I SESS          tmux list-windows  -t SESS          -F &#39;SESS:##{window_index}&#39; \
</span></span></span><span class="line"><span class="cl"><span class="s2">              | xargs -I SESS_WIN      tmux list-panes    -t SESS_WIN      -F &#39;SESS_WIN.##{pane_index}&#39; \
</span></span></span><span class="line"><span class="cl"><span class="s2">              | xargs -I SESS_WIN_PANE tmux send-keys     -t SESS_WIN_PANE &#39;%1&#39; Enter\&#34;&#34;</span><span class="err">
</span></span></span></code></pre></div><div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>I have set my tmux prefix to <kbd>C-z</kbd>.&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
]]></content><category scheme="https://scripter.co/categories/unix" term="unix" label="unix"/><category scheme="https://scripter.co/tags/tmux" term="tmux" label="tmux"/><category scheme="https://scripter.co/tags/pane" term="pane" label="pane"/><category scheme="https://scripter.co/tags/window" term="window" label="window"/><category scheme="https://scripter.co/tags/session" term="session" label="session"/></entry></feed>