Tmux keybinding to swap pane left or right

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'**


: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.


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.