使用fzf-tmux进行快速窗口切换的tmux脚本给了我错误的选择 [英] tmux script for fast window switching with fzf-tmux gives me the wrong options

查看:351
本文介绍了使用fzf-tmux进行快速窗口切换的tmux脚本给了我错误的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下内容,使我可以使用fzf切换tmux窗口:

tmux list-windows -F "#I:#W" | fzf-tmux | cut -d ":" -f 1 | xargs tmux select-window -t

当我在shell中运行它时,它可以完美运行,为我提供了一个fzf窗口列表,我可以搜索并切换到该窗口.

我将其绑定到tmux中的f键:

# 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"

但是当我运行它时,它会显示相同的窗口N次(其中N是我打开的窗口数).

因此,如果我打开了3个窗口,则在zsh中运行脚本会给我:

1. first window
2. second window
3. third window

tmux键绑定给我的地方

1. first window
1. first window
1. first window

我以前没有写过tmux脚本,所以也许有比我在这里做的更好的获取Windows的方法?否则,我不明白为什么从tmux运行并在shell中运行时为什么会得到不同的选择.

更新:

似乎run-shell "tmux list-windows"正确输出了不同的窗口(但带有大量我不想要的布局信息),而run-shell "tmux list-windows -F '#I:#W'"给了我一个原来的问题,即对于每个窗口条目都重复相同的窗口./p>

我认为问题在于tmux正在将#I:#W扩展为当前窗口,而不是将其传递给list-windows命令,因此我需要对其进行转义.有关如何执行此操作的任何想法?

更新2:

答案是对变量(##I:##W而不是#I:#W)进行双散列.

解决方案

万一将来有人要使用此脚本,请使用正确的版本:

# 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"

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

解决方案

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"

这篇关于使用fzf-tmux进行快速窗口切换的tmux脚本给了我错误的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆