9

I'm aware that :swap-pane -U and :swap-pane -D will swap panes up or down, but I'd like a keybinding to swap panes left and right.

The only way I can think of to do this would be to first determine the pane index of the current pane, then run :select-pane -R or -L and determine its index, and finally run :swap-pane -s (first pane index) -t (second pane index).

I'm just not sure how to do this in .tmux.conf - is it possible to have subcommands which return something within another command?

For example, something like this:

bind-key -n C-{ swap-pane -s **get current pane ID** -t **get pane ID of 'select-pane -L'**

bind-key -n C-} swap-pane -s **get current pane ID** -t **get pane ID of 'select-pane -R'**

2 Answers 2

18
+50

:bind-key -n C-{ select-pane -L \; swap-pane -s '!'

This should tell tmux to select the left pane, and then swap with the previously active pane.

Also, swap-pane -s {right-of} seems to work.
Here is the list of ways to refer to other panes:

{last} !       The last (previously active) pane
{next} +       The next pane by number
{previous} -   The previous pane by number
{top}          The top pane
{bottom}       The bottom pane
{left}         The leftmost pane
{right}        The rightmost pane
{top-left}     The top-left pane
{top-right}    The top-right pane
{bottom-left}  The bottom-left pane
{bottom-right} The bottom-right pane
{up-of}        The pane above the active pane
{down-of}      The pane below the active pane
{left-of}      The pane to the left of the active pane
{right-of}     The pane to the right of the active pane

See the online man page for reference.

4
  • Awesome! I had no idea that you could reference those variables, that's super useful. I'll award the bounty as soon as it allows me to, thanks! I was referring to this man page which I guess is probably outdated because my local tmux manpage lists these variables while that one doesn't.
    – HellaMad
    May 25, 2016 at 5:55
  • 1
    Great! this is the man page I used. also has similar info for "windows" in addition to "panes". May 25, 2016 at 11:55
  • 1
    Those variables are a god-send. How come so few answers and tutorials mention them, I wish I knew about this earlier.
    – lleaff
    Feb 17, 2017 at 14:59
  • 1
    it should be swap-pane -s '{right-of}'
    – CodyChan
    Apr 16, 2021 at 14:02
3

One can also add following line to ~/.tmux.conf

bind v swap-pane -s '!' -t $TMUX_PANE

This will interchange current pane with the last visited pane. Consecutively go to the two panes needed to be interchanged and use <prefix>v (above command) to perform the swap. Note that <prefix>q shows the id of the panes. The additional advantage is that the swapping also works across different windows.

1
  • Very clean :+1:
    – moeabdol
    May 4, 2022 at 22:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .