42

In tmux, how can I move a window from a session to another session?

ex. move window:4 in session [0] to session [4] .

黃郁暉
  • 437
  • 1
  • 4
  • 7
  • Possible duplicate of [Move window between tmux clients](https://stackoverflow.com/questions/3094946/move-window-between-tmux-clients) – exhuma May 11 '18 at 12:37
  • @chepner might want to remove their comment, as the accepted answer is correct and solves the OP's problem. – b-jazz Mar 18 '20 at 20:10
  • Good point; not sure what I was thinking of when I posted that. – chepner Mar 18 '20 at 20:13

2 Answers2

70

From my testing on tmux 2.6, you'll need two things for the command to move an entire window over:

  • The name of the session you want to move the window from (for future reference, $session_name)
  • The index of the window you want to move (in the session it's currently in, of course -- we'll call this $window_index). This is actually optional -- if you omit this, then it defaults to the window in focus in the session you're pulling the window from.

From this point, you can just change to the session you want to move the window into, <tmux-escape>: into a command prompt, and type a command of this form:

move-window -s $session_name[:$window_index]

...where, as noted before, the $window_index is optional (as indicated by the square brackets, which aren't actually part of the syntax ). To use some concrete examples:

# Moves from currently-focused window from session named `$session_name`
move-window -s $session_name 
# Moves from window with index `$window_index` from 
# session named `$session_name` into the current session
move-window -s $session_name:$window_index

Et voilà! Your window got moved. :)

EDIT: Added some more info on an alternative that omits $window-index.

ipatch
  • 3,933
  • 8
  • 60
  • 99
Erich Gubler
  • 1,105
  • 10
  • 12
  • 10
    I struggled with this for a bit before figuring out my problem. I often leave the initial session unnamed and was trying to specify the session name as just the index (0). If you want to move from an unnamed session, precede your session number with a dollar sign ($). – b-jazz Mar 18 '20 at 20:13
  • for the OP example `move-window -s 0:4 -t 4` – Tun Jun 10 '20 at 04:18
  • 1
    Not sure which version this was released in, bur there's now a built-in shortcut `.` to bring up the `move-window` command, so it's as easy as `. :`. – iforwms Mar 16 '22 at 06:29
  • @iforwms: I'd encourage you to submit another answer! Your solution definitely seems simpler, and it shouldn't be too hard to figure out which version of `tmux` it applies to. – Erich Gubler Mar 22 '22 at 02:22
  • This seems to move some random window from the session name, not to the session name – Danielo515 Apr 28 '23 at 08:46
  • @Danielo515: The intent of the instructions I gave is that you are moving from the session where the window currently exists to the session you are currently in. Is there an edit you can suggest to make that more clear? – Erich Gubler Apr 29 '23 at 15:45
10

There's now a built-in shortcut <tmux-escape>. to bring up the move-window command, so it's as easy as <tmux-escape>. <session_name>:<window_index> or to move a window within the same session, omit the session name: <tmux-escape>. <window_index>

iforwms
  • 331
  • 2
  • 9