Load terminal from another window into current split pane in tmux

from–https://superuser.com/questions/1485934/load-terminal-from-another-window-into-current-split-pane-in-tmux

Asked 
Viewed 2k times

2

In GNU Screen, I can switch to any window number within a given split. For example:

  • Enter screen to launch it.
  • Press Ctrl + a c to create a new window. Now there are two windows: window 0 and window 1. The current window is now window 1.
  • Press Ctrl + a Shift + s to create a new split.
  • Press Ctrl + a Tab to go to the new split.
  • Press Ctrl + 0 to load window 0 in this new split.

How can we do a similar thing in Tmux? This is what I have so far:

  • Enter tmux to launch it.
  • Press Ctrl + b c to create a new window. There are two windows now: window 0 and window 1. The current window is window 1.
  • Press Ctrl + b " to create a new split pane. The current pane is now the new split pane.
  • How can I now load the shell in window 0 into this new split pane?

Is there a way to simulate this behavior of screen in tmux?

1 Answer

3

Tmux does not support displaying the same pane in two different windows nor does it support displaying the same pane twice in the same window. You can link a whole window to a session without unlinking it from its original session. Or you can place a pane in another window but this will remove the pane from its original window.

In your example you have already created a pane that holds the place for the desired pane. In this case you can swap the two panes with swap-pane tmux command. From man 1 tmux:

swap-pane [-dDU] [-s src-pane] [-t dst-pane]
(alias: swapp)

Swap two panes. […] -d instructs tmux not to change the active pane.

If -s is omitted and a marked pane is present (see select-pane -m), the marked pane is used rather than the current pane.

It’s even better: if -s and -t are both ommited and a marked pane is present, the marked pane and the current pane are swapped. At any given time there can be at most one marked pane within the entire tmux server.

How can I now load the shell in window 0 into this new split pane?

Navigate to the window 0 and mark the pane you want to move (in your example it’s the only pane there). You do it with RMB (if mouse is supported) or prefix m (where default prefix is Ctrl+b). Navigate back to the new pane and invoke:

tmux swapp

The shell where you invoke tmux swapp is not automatically destroyed, it’s now in the window 0 where the marked pane used to be. In your example it seems you don’t need this shell itself, you just need the new pane; if so, consider tmux swapp && exit instead. Keep in mind if there is no marked pane (or if the current one is marked) then tmux swapp will effectively do nothing. In such case tmux swapp && exit will just exit the shell.

Alternatively, instead of creating a new split pane you can move the desired pane into the current window in one step.

join-pane [-bdhv] [-l size | -p percentage] [-s src-pane] [-t dst-pane]
(alias: joinp)

Like split-window, but instead of splitting dst-pane and creating a new pane, split it and move src-pane into the space. […]

If -s is omitted and a marked pane is present (see select-pane -m), the marked pane is used rather than the current pane.

So instead of prefix ":

  1. Mark the pane you want to move.
  2. Split the pane you want to split:
    • either with tmux joinp -v invoked in the shell there;
    • or with prefix : joinp -v Enter; in particular this is useful when there is no idle shell in the pane you want to split (but don’t forget to make the pane current first).

The method with prefix : can be used to invoke swapp or any other tmux command.

Below there are few notes about manipulating panes from the outside of tmux. The examples use tmux swapp but you can invoke tmux joinp similarly.


swap-pane allows you to swap any two panes within a tmux server. You can run tmux swapp even from the outside of tmux, without manually marking and changing panes, if you know how to address the two panes you want to swap. It may be:

tmux swapp -s 0:1.1 -t 0:0.0

The format is session:window.pane, see the manual where it explains target-pane. Note these bare numbers are “relative” (at least the :window.pane part, I’m not sure about the session number). The very first pane 0:0.0 after swapping with 0:1.1 becomes 0:1.1. On the other hand tmux sessions, windows and panes have their unique identifiers (prefixed with $@ and % respectively). In your case the command may be like:

tmux swapp -s %2 -t %0

But note if the old pane was indeed %0 at 0:0.0 and the new one was %2 at 0:1.1 then they are now %0 at 0:1.1 and %2 at 0:0.0 respectively. The unique identifier of each pane is “attached” to the content (process) of the pane and “travels” with it. Non-prefixed identifiers of panes depend on total number of panes and their placement within the window.

You can name sessions (rename-session) and windows (rename-window). This makes identifying panes with session:window.pane easier.


You can run more than one tmux server. Servers are identified with their socket paths (-L option) or socket names (-S). The default name is default. If you invoke tmux without these options from within tmux then it will find the right server (via the TMUX environment variable, I think). If you invoke tmux without these options from the outside of tmux then it will be like tmux -S default. You may want to swap two panes that belong to a non-default tmux server, e.g.:

tmux -S other swapp -s 0:1.1 -t 0:0.0

Tmux doesn’t support swapping panes between servers. The whole point of having two servers is they are separate. If you ever need to swap panes between two semi-separate entities of tmux, these entities should be sessions within a single tmux server. You can swap panes between different sessions.

If you really need to “hijack” a process running under another tmux server, use reptyr. The tool is totally independent from tmux.

上一篇
下一篇