3

It seems like a simple enough procedure that I'm trying to accomplish, yet I've searched to no avail. I would like to create a key binding which does a split-window and then send-keys to the newly-created pane; however I need the pane id to use with the send-keys command. The catch is that I WILL NOT know how many panes are currently open in the window; thus I know of no way for the code running in the original pane to deduce what the new pane index will be. Is there any way to find out this new index or id (either one can be used as a target)?

Thanks.

0

You are over-thinking your problem. When you do split-window the new pane becomes the current target for send-keys. You should not be calling tmux in the binding as you are already in tmux. Try

bind-key s split-window \; send-keys "pwd" Enter
  • That was the first thing I tried, actually, but the "pwd" was (and still is after trying again just now) printed in the original pane, not the new one; behaving as if I had given it the "-d" argument, but I did not... I used the exact same binding you have written above. Maybe there's something else about my setup (tmux.conf or plugins) that's changing the behavior to not switching the pane focus after creation? – Kyndig Jul 10 '17 at 22:04
  • I have tmux version 2.1 (tmux -V) – meuh Jul 11 '17 at 7:45
  • 2.4 here. Does anyone have any ideas about how I can diagnose why split-window behaves as if I passed the -d argument when I haven't? – Kyndig Jul 12 '17 at 17:11
2

Another way, especially useful for shell scripts.

tmux split-window -P -F "#{pane_id}"

will output the pane id, (e.g. %4) into stdout

so you can do

bash_var=$(tmux split-window -P -F "#{pane_id}"

if you want to capture it in a shell/bash variable.

Taken from here: https://github.com/tmux-plugins/tmux-sidebar/blob/master/scripts/toggle.sh

1

In tmux, each new pane gets an unique value, which you can access using environment variable TMUX_PANE. tmux display -pt "${TMUX_PANE:?}" '#{pane_index}' this will show pane number.

  • But that tmux display command would have to be run from the new pane in question. Calling it from the original pane which the keybinding was executed in would just give me the index of the pane I'm already in. So how would I execute it from the new pane and communicate the resulting output back to the original pane? If I could do that, that would mean I had already gotten into the new pane. To be more concrete; this is what I currrently have been trying to do in my .tmux.conf: – Kyndig Jul 6 '17 at 18:59
  • Sorry, I ran out of edit time on the above comment. This is basically what I'm trying to do in my .tmux.conf: "bind s tmux split-window \; send-keys -t $new_pane_id -l "run_this_command" \; send-keys -t $new_pane_id Enter " But I don't know how to get $new_pane_id... – Kyndig Jul 6 '17 at 19:12
1

Well, I'd still prefer to find a direct way to get at the new pane ID, but I was at least able to accomplish my goal by calling a shell script instead of trying to do it all in the tmux.conf. Seems kludgy to me, but it works. tmux.conf:

bind s run-shell "~/bin/tmux_split_pane.sh '#{window_id}' '#{pane_id}'"

and the script:

#!/bin/sh

current_window_id=$1
current_pane_id=$2

tmux split-window -t $current_pane_id
new_pane_id=$(tmux list-panes -F '#{pane_id}' -t "$current_window_id" | sort -n --key=1.2 | tail -1)
tmux send-keys -t $new_pane_id -l "update_env" \; send-keys -t $new_pane_id Enter
0

I did it by sending the new pane's id back through a named pipe.

mkfifo pane_id
tmux split-window -h \; send-keys 'echo $TMUX_PANE > pane_id' Enter \; select-pane -t "$TMUX_PANE"
cat pane_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.