Commanding Devices with Tmux

Let’s go right away to the question: How do I enter commands on two devices at the same time?

One of the possible answers is: With tmux.

My usual workflow to connect to the end devices is from a Linux host that has tmux installed. The situation can look for example like this:

Before being able to send the commands to both of the panes at the same time we need to get the pane “coordinates”. Let’s open a third pane and use the tmux list-panes command there:

It lists all the panes in the current tmux window. You remember: panes are the areas within the window.

The command shows some information about the panes, like their sizes and the number of lines in the backscroll buffers, but the interesting data this time is the pane index. That’s the first number in the line.

Another way to show the pane indexes briefly is using Ctrl-a q command:

(You have read my earlier Using Tmux post, haven’t you? Then you know that my tmux command key is Ctrl-a, not Ctrl-b…)

Ok, so the device panes are 0 and 1.

Now we can use the pane indexes to send keys (or commands) to those panes. Let’s try it:

tmux send-keys -t .0 "abc"

Wow, it worked! See the manual page of tmux for more details about the -t (target) option.

The command only sent the letters a, b and c there though, so let’s try something else:

tmux send-keys -t .0 Enter
tmux send-keys -t .0 "ping -c 3" Enter

So now I know how to send full commands to a single pane.

How about sending the same command to multiple panes? Unfortunately I didn’t find a way to do that with a single tmux command, but a little shell scripting does the job:

for PANE in 0 1; do tmux send-keys -t .$PANE "ping -c 3" Enter; done

Looks a bit complex, but actually this can also be considered a basic building block that can be used in more complex scenarios as well.

Happy tmuxing!

Updated: March 20, 2024 — 20:24


Add a Comment
  1. If the command I want to run is identical (like in your example), I just temporarily set synchronize-panes to “on” and all the keystrokes from one pane are mirrored on all the other panes.

    1. Thank you! I had totally forgotten that simple trick! I even have a command defined for that in my .tmux.conf 😂

Leave a Reply