Search code examples
tmux

How to move tmux window left or right?


My goal: bind the keys Ctrl+Shift+PgUp[PgDn] to increase[decrease] current window's index by 1 in TMUX. In order to achieve a behavior similar to browser-like tab moving. Looks like a simple issue, and maybe i over complicate things.

Problem: swap-window command does not have an -F flag option, thus it doesn't recognize tmux formats (expressions)

I've tried:

user@host ~> # my current window index is 1
user@host ~> tmux display -p '#I'
1
user@host ~> tmux set -F @next_window '#{e|+|:#I,1}'
user@host ~> tmux display -p '#{@next_window}'
2

I am trying to use 2 things here:

  1. From tmux's man page:

    Certain commands accept the -F flag with a format argument. This is a string which controls the output format of the command. Format variables are enclosed in ‘#{’ and ‘}’, for example ‘#{session_name}’. The possible variables are listed in the table below, or the name of a tmux option may be used for an option's value. Some variables have a shorter alias such as ‘#S’; ‘##’ is replaced by a single ‘#’, ‘#,’ by a ‘,’ and ‘#}’ by a ‘}’.

  2. tmux's wiki on expressions. E.g. to substract 5 from 10 in a format you do #{e|-|:10,5}

It'll be logical to assume that we just need to add the following to our tmux.conf:

# reorder windows
set -f @next_window '#{e|+|:#I,1}'
set -f @previous_window '#{e|-|:#I,1}'
bind C-S-PgUp swap-window -d -t '#{@next_window}'
bind C-S-PgDn swap-window -d -t '#{@previous_window}' 

However, as i already mentioned swap-window does not have an -F flag option, so swap-window -d -t '#{e|-|:#I,1}' would just produce can't find session: #{e|-| and ofc swap-window -d -t '#{@foo}' says can't find session: #{@foo}


Solution

  • This works for me with tmux 3.3a (I'm on Mac so not using PgUp / PgDn)

    bind -n C-Left  run 'tmux swap-window -d -t #{e|-|:#I,1}'
    bind -n C-Right run 'tmux swap-window -d -t #{e|+|:#I,1}'