How does one swap two panes in Tmux?

228

42

I have two panes in horizontal split. I'd like to have the pane currently on the right to be on the left in the window. I'm not talking about moving the focus (Ctrl+B o). How do I achieve this?

'left pane' <--> 'right pane'

oddRaven

Posted 2015-02-17T22:27:28.470

Reputation: 2 475

Answers

338

The swap-pane command can do this for you. The { and } keys are bound to swap-pane -U and swap-pane -D in the default configuration.

So, to effect your desired change, you can probably use Prefix { when you are in the right pane (or Prefix } if you are in the left pane).

The -U and -D refer to “up” and “down” in the pane index order (“up” is the same direction that Prefix o moves across panes). You can see the pane indices with display-panes (Prefix q, by default).

Chris Johnsen

Posted 2015-02-17T22:27:28.470

Reputation: 31 786

9+1 for specifying Prefix instead of assuming everyone will use Ctrl-b – Aaron Wallentine – 2017-10-05T15:55:02.700

2Agreed. It's even worse when people assume you're using Ctrl-a, since they're the people who know that the prefix can be changed to suit the user. – byxor – 2017-10-13T10:46:44.813

See @kay's response for more general use of swap-pane (and answer to question in title). – Jonathan – 2018-12-19T19:18:08.930

60

You can hit Ctrl b and keep holding down Ctrl while hitting o. This will rotate all existing panes around, so in your case it will swap the only two existing panes.

Ctrl b + Alt o rotates the opposite way (useful when you have more than two panes).

joelostblom

Posted 2015-02-17T22:27:28.470

Reputation: 1 833

1Yeah, we're on the same page. But after the first 'o', which does cause a pane rotation, the next 'o' is sent to the underlying shell and processed by whatever app be there. I'm thinking something fishy with my map, or maybe something that's dependent on tmux version.

Thanks for responding. – Stabledog – 2017-02-14T17:34:26.043

1@Stabledog I'm on tmux 2.3 currently, but I think this has always worked for me... I can't really think of what could be the issue. Sounds strange that the Ctrl+o is being sent to the shell instead of to tmux, especially if this is not the case for repeatedly pressing Ctrl+b (or other tmux commands?). – joelostblom – 2017-02-14T23:45:47.173

Actually I think it is happening for all commands. At least, I'm not aware of any case where I can do Ctrl+x, hit a key that's bound to some operation, and stay in "tmux keyboard" mode. I just assumed that's by design. – Stabledog – 2017-02-15T17:45:36.070

1@Stabledog Actually, I just noticed that the only other command where I can keep holding down control and repeat the other keys is Ctrl+b;, which keeps rotating the cursor between two panes. – joelostblom – 2017-02-16T02:58:47.623

35

The most precise control you can have is by using the command swap-pane directly. This is not so difficult to do:

  1. ctrl-b q shows you the "ID" for each pane in current window - remember the two panes you want to swap. Let's say they're 3 and 5.
  2. ctrl-b : to activate the tmux command line. Then issue command swap-pane -s 3 -t 5.

Note that you have auto-completion when typing commands. Also you can search for syntax of a command directly from tmux manpage. That's how I learn the syntax for swap-pane.

Kay

Posted 2015-02-17T22:27:28.470

Reputation: 681