I'm using ZSH and am trying to integrate FZF into my config. It all works fine now, the only thing bothering me is that after searching for the command I want to run in FZF (e.g. a file/directory name) and pressing <ENTER>
to close FZF, I have to press <ENTER>
again to run the command.
我正在使用ZSH,并试图将FZF集成到我的配置中。现在一切正常,唯一困扰我的是在FZF中搜索我想要运行的命令(例如文件/目录名)并按
键关闭FZF后,我必须再次按
键才能运行该命令。
Is there a way to run the command automatically after selecting an FZF result?
有没有办法在选择FZF结果后自动运行该命令?
EDIT:
Thanks to @svlasov I now have this solution:
编辑:多亏了@svlasov,我现在有了这个解决方案:
export FZF_COMPLETION_TRIGGER=""
setopt vi # Removes the default FZF '<TAB>' keybinding
fzf-and-run-widget() {
fzf-completion
zle accept-line
}
zle -N fzf-and-run-widget
bindkey '^[^I' fzf-and-run-widget
更多回答
Depends on what "run" means to you. For simple shellscript execution (like ./myscript.sh
), you could prepend ./
to the chosen file and have fzf on pressing Enter replace itself with that command using become
, e.g. as fzf --bind "enter:become(./{})"
. Adapt ths to xdg-open
or whatever you consider "running" your chosen item.
这要看“逃跑”对你来说意味着什么。对于简单的外壳脚本执行(如./myscript.sh),您可以在所选文件前面加上./,并在按Enter键时让fzf用该命令替换其自身,例如fzf--绑定“Enter:By(./{})”。将其修改为xdg-open或任何您认为“运行”您选择的项目。
优秀答案推荐
Add to .zshrc
:
添加到.zshrc:
fzf-and-run-widget() {
fzf-history-widget
zle accept-line
}
zle -N fzf-and-run-widget
bindkey '^R' fzf-and-run-widget
fzf-history-widget
is part of fzf distribution, defined in key-bindings.zsh
. Replace with your own, if needed.
zle accept-line
executes whatever is currently in ZSH prompt.
'^R'
is Ctrl-R. Change to something else, if needed.
更多回答
Thanks that looks like what I was searching for! Do you know which widget I would use to get the "normal" fzf completion? (I mean the one triggered by typing **<TAB>
)
谢谢!这看起来就是我要找的东西!你知道我会用哪个小工具来得到“正常”的fzf补全吗?(我指的是通过键入**触发的命令)
It must be fzf-file-widget
.
它必须是fzf-file-widget。
I already tried that one, but it only does files. I mean the one that adjusts to the command typed (for example, when I type ssh **<TAB>
it gives me a list of hostnames to choose from)
我已经试过那个了,但它只支持文件。我指的是根据输入的命令进行调整的命令(例如,当我键入ssh**时,它会给出一个可供选择的主机名列表)
Try fzf-completion
.
尝试使用fzf-Complete。
I fixed it; the problem was that fzf-completion
still checks for the trigger (i.e. '**') in the console. As I didn't have a blank String as the Trigger, it was automatically quitting as it didn't find that. I added my resulting snippet to the original Post. Thanks again!
我修复了它;问题是fzf完成仍然检查控制台中的触发器(即‘**’)。因为我没有空字符串作为触发器,所以它自动退出,因为它没有发现这一点。我将生成的代码片段添加到原始帖子中。再次感谢!
我是一名优秀的程序员,十分优秀!