Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tmux script for fast window switching with fzf-tmux gives me the wrong options

Tags:

tmux

fzf

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.

Update:

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?

Update 2:

Answer is to double hash the variables (##I:##W instead of #I:#W).

like image 255
GTF Avatar asked Jun 09 '16 16:06

GTF


2 Answers

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"

like image 86
peterfication Avatar answered Oct 02 '22 21:10

peterfication


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"
like image 41
GTF Avatar answered Oct 02 '22 21:10

GTF