I've written the following which allows me to switch tmux window with fzf:
tmux list-windows -F "#I:#W" | fzf-tmux | cut -d ":" -f 1 | xargs tmux select-window -t
When I run this in a shell it works perfectly, giving me an fzf
list of windows which I can search through and switch to.
I bound it to the f
key in tmux:
# fast window switching
unbind f
bind-key f run "tmux list-windows -F \"#I:#W\" | fzf-tmux | cut -d \":\" -f 1 | xargs tmux select-window -t"
But when I run it, it displays the same window N times (where N is the number of windows I have open).
So if I have 3 windows open, running the script in zsh gives me:
1. first window
2. second window
3. third window
where the tmux key binding gives me
1. first window
1. first window
1. first window
I haven't written tmux scripts before, so perhaps there's a better way of getting the windows than what I am doing here? Otherwise, I don't understand why I get different options when run from tmux and run in a shell.
it seems that run-shell "tmux list-windows"
correctly outputs the different windows (but with a load of layout info that I don't want), whereas run-shell "tmux list-windows -F '#I:#W'"
gives me the original problem of the same window repeating for each window entry.
I think that the problem is that tmux is expanding #I:#W
for the current window, instead of passing it to the list-windows
command, so I need to escape them. Any ideas on how to do that?
Answer is to double hash the variables (##I:##W
instead of #I:#W
).
Thanks for sharing your solution! Helped me a lot :)
For people who come across this post and wonder how this command can be used to switch between tmux sessions:
bind-key C-f run-shell "tmux list-sessions -F \"##S\" | fzf-tmux | xargs tmux switch -t"
In case anyone wants to use this script in the future, the correct version this:
# fast window switching
bind -n C-f run-shell "tmux list-windows -F \"##I:##W\" | fzf-tmux | cut -d \":\" -f 1 | xargs tmux select-window -t"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With