Search code examples
tmux

TMUX 3.2a using send-keys executes the command in the wrong pane


I'm trying to setup a shell script that will start a Tmux session, create 2 panes and name them (and eventually run an additional command). The issue I'm running into is the send-keys seems to send the command to the correct pane, but it doesn't seem to actually execute the command in that pane.

#!/bin/sh

session="testing"

# set up tmux
tmux start-server
tmux new-session -d -s $session
tmux set pane-border-status top

# Name pane 0
tmux send-keys -t 0 "tmux select-pane -T Unit/Alarm1" Enter

# Split pane 0 horizontal by 34%, name pane 1
tmux splitw -h -p 67
tmux send -t 1 "tmux select-pane -T Unit/Alarm2" Enter

tmux attach-session -t $session

After running the script these are the results I get.

script results

What am I missing?

I tried using C-m instead of Enter but have the same result.

When I do the steps manually (not using the script) I find that tmux send-keys -0 "..." Enter will send the keys to the correct pane, but it executes the command on the current pane (if I'm in pane 1, it renames pane 1, not pane 0).

Manual steps before executon

Manual steps after execution


Solution

  • You can send keys to a pane that isn't the active pane, but select-pane command doesn't refer to the pane the command is run in. By default, select-pane will refer to the active pane. I expected that the pane you most recently created was the active pane, but it's possible there's a race condition between your send-keys command returning and your next command executing, or that something else is going on that I don't understand.

    Regardless, you can avoid these issues (and make your script simpler) by using -t to point to the right pane, like

    # Name pane 0
    tmux select-pane -t .0 -T "Unit/Alarm1"
    
    # Split pane 0 horizontal by 34%, name pane 1
    tmux splitw -h -p 67
    tmux select-pane -t .1 -T "Unit/Alarm2"
    

    If you want to be more specific, you can target different tmux sessions or windows with select-pane, like

    tmux select-pane -t $SESSION:$WINDOW.$PANE -T "$PANE_NAME"