0

I'm trying to capture the current pane in a variable. But, tmux run "#{pane_id}" overrides the current pane with the command output, and then clears it after I hit a key. My remote machine prints the same command output to the next line of the pane. How do I change the behavior so my local machine prints to the next line?

I'm trying to just capture the command output in a variable so I can change pane switching behavior depending on the pane number.

Terminal prior to command: enter image description here

Terminal after command: enter image description here

After hitting a key I return to the terminal prior to the command.

2 Answers 2

1

You don't want to execute the pane id itself, you want to execute a command that outputs the pane id.

e.g.

$ var=$(tmux run "printf '%s' '#{pane_id}'")
$ echo "$var"
%2
1
  • Thanks. That gets rid of the error code. But, I'm still printing to a separate pane. Let me add a screen shot.
    – ZeroPhase
    Jun 9, 2022 at 4:30
1

I'm not addressing the title; I'm not trying to fix your tmux run … (compare XY problem). I'm addressing what seems to be your actual problem:

I'm trying to just capture the command output in a variable so I can change pane switching behavior depending on the pane number.

A process running in a pane should already have TMUX_PANE in its environment. Examples: echo "$TMUX_PANE", env | grep TMUX. This means a shell script in a pane can use $TMUX_PANE directly as a variable. I'm not sure if you want to get the ID in the pane itself or elsewhere though.

A more general way is:

tmux display-message -p '#{pane_id}'

and you can capture this to a shell variable with:

var="$(tmux display-message -p '#{pane_id}')"

The advantages are:

  • The command works in a shell outside tmux (where you obviously cannot use $TMUX_PANE).
  • Works if inside tmux, yet not in a pane (e.g. prefix:display-message '#{pane_id}'Enter; the default prefix is Ctrl+b). This includes code (possibly shell code) you may want to run upon a keystroke (tmux bind-key …).
  • display-message accepts -t, so even in a pane you are not limited to the ID of the pane itself. Example: tmux display-message -p -t '{bottom}' '#{pane_id}'. See man 1 tmux where it describes target-pane.
1
  • That does fix my ability to use the `#{pane_id}' in a variable. I'm still getting an issue where the command prints to a temporary pane. I've solved this before by passing quiet flags for other commands. But, my remote tmux session doesn't have the same behavior.
    – ZeroPhase
    Jun 9, 2022 at 4:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .