1

this question (to my knowledge) has not been asked before, yet would benefit anyone that uses tmux! I tried searching github too for plugins etc, but no luck yet.

What I'd like to achieve:

  • Cycle between windows of the same name.

Why?

Imagine you have 6 tmux windows, in the following order, status bar would look similar to this:

[0:zsh][1:vim][2:zsh][3:vim][4:zsh][5:vim]

If Im currently in window 0 (zsh):

  • I would like to cycle between the 3 'zsh' windows (0,2,4)

If Im currently in window 1 (vim):

  • I would like to cycle between the 3 'vim' windows (1,3,5)

This would allow you to cycle windows of the same type without having to re-order all your windows first (vim next to vim, zsh next zsh etc). bliss!

I have tried myself, but no success :(

1
  • Why do you want to cycle, rather than go directly to a particular one? Commented Apr 27, 2021 at 6:13

2 Answers 2

3

I created a basic solution.

  1. Save the following script as _tmux-cycle-samename and make it executable (chmod +x _tmux-cycle-samename).

    #!/bin/sh
    
    if [ "$1" = "-r" ]; then filter=tac; else filter=cat; fi
    
    name="$(tmux display-message -p '#W' | sed 's|\(.\)|[\\\1]|g')"
    
    tmux select-window -t "$(
       tmux list-windows -F '#{window_active} #{window_id} #W' \
       | grep '^[01] @[0123456789]* '"$name"'$' | "$filter" \
       | awk '
          NR==1 {result=$2}
          { if (seen==1) {result=$2; exit} 
            seen=$1
          }
          END {print result}
       '
    )"
    

    The script retrieves the right name (tmux display-message …) and prepares the string (sed …), so when interpreted as regex later the name is matched literally. Then the script lists windows (tmux list-windows …), picks the matching ones (grep …), preserves or reverses the order (cat or tac from the expansion of $filter) and finds the next inactive window (awk …). Finally the found window is selected (tmux select-window …).

  2. Add these to your ~/.tmux.conf:

    bind-key -T prefix > run-shell '/full/path/to/_tmux-cycle-samename'
    bind-key -T prefix < run-shell '/full/path/to/_tmux-cycle-samename -r'
    

    If _tmux-cycle-samename can be resolved via PATH then you don't need to specify the full path.

  3. If already inside tmux then run tmux source-file ~/.tmux.conf. A tmux server started anew will source the file automatically.

  4. Try prefix> and prefix< in your tmux to test the solution (the default prefix is Ctrl+b).

1
  • Thank you Kamil! sorry for the delayed response, it works perfectly and only 10 lines of posix code too! thank you so much! marking this as accepted and a +1 Commented May 2, 2021 at 11:28
0

There is a command to switch to the last visited pane: ctrl-b + ;

You can bind the # key (or anything you like) to switch to the last visited tmux pane. I chose # because it's like how:b# switches to the last buffer in vim:

# go to last pane (like b# in vim)
bind '#' select-pane -l

So you can just press you tmux prefix (by default ctrl-b) followed by #.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.