Endlisnis
Endlisnis

Reputation: 519

tmux split-window without changing focus

Is there any way to split a window in tmux without changing the current focus?

I'm running a script inside one of my tmux panes that occasionally runs "tmux split-window ..." with some command that takes a minute to complete and MAY request input.

I can end up trying to type input into one of the tmux panes but in the middle of my typing, the original pane executes "tmux split-window ..." and (mid word) my cursor shifts to the new pane, and I end up typing part of the input into the wrong pane.

Upvotes: 6

Views: 2874

Answers (1)

user777337
user777337

Reputation:

Note: this answer is correct, but obsolete. The right way is to use -d flag for split-window command. I'm leaving this answer as a demonstration how to do some yak shaving with tmux.

A split-window command flag provided by tmux would be the right solution for this. Unfortunately tmux does not provide such command flag. Update: there is a -d split-window flag that does this.

  • The simple solution is to immediately switch to previous pane after split-window:

    tmux split-window
    tmux last-pane
    

    This can be also written as a one liner:

    tmux split-window\; last-pane
    

    The downside of this solution is that *theoretically* you might end up writing a character in the wrong window if you type it in time interval between split-window and last-pane command execution.

  • Here another approach with the downside that it's more complex.

    Create a new window in the background and get the pane_id of this window (note how this command is wrapped in $(...) because we want it executed in a subprocess:

    pane_id=$(tmux new-window -d -P -F "#{pane_id}")
    

    Now join the window we just created with the window where your cursor is located (will not change cursor focus):

    tmux join-pane -b -t "$pane_id"
    

    Add -h to the join-pane above if you want a horizontal split.

I recommend taking the first approach for it's simplicity. It's highly unlikely you'll have any practical issues with it.

Upvotes: 10

Related Questions