fish shell 自定义函数上的自动完成文件
autocomplete files on fish shell custom function
我正在从 ZSH 迁移到 Fish,我唯一没弄清楚的是我的 c
函数:
functions/c:
#!/bin/sh
cd "$PROJECTS/"
functions/_c:
#compdef c
_files -W $PROJECTS -/
这在 ZSH 上工作得很好,我可以输入 c <tab>
并且它完成就像我直接输入 cd $PROJECTS
一样。
在鱼上,我创建了一个 c.fish
这样的:
function c
cd $PROJECTS/$argv
end
complete --command c --no-files --arguments='(find $PROJECTS -mindepth 1 -maxdepth 2)'
但是正如您所想象的,它不能像 ZSH 版本那样工作,因为完成不知道 $argv
,并且不会完成第一个文件夹。
有没有办法在 Fish 中做同样的事情?
我考虑过创建一个 abbr
,但我真的很喜欢我今天在 ZSH 上使用它的方式。
如果 c
应该像 cd
一样将 $PWD
设置为 $PROJECTS
,这是一种方法:
function c_complete
# get the argument to 'c'
set arg (commandline -ct)
# save our PWD
set saved_pwd $PWD
# cd to $PROJECTS (and then back after)
# while in $PROJECTS, complete as if we are 'cd'
builtin cd $PROJECTS
and complete -C"cd $arg"
builtin cd $saved_pwd
end
complete --command c --arguments '(c_complete)'
我正在从 ZSH 迁移到 Fish,我唯一没弄清楚的是我的 c
函数:
functions/c:
#!/bin/sh
cd "$PROJECTS/"
functions/_c:
#compdef c
_files -W $PROJECTS -/
这在 ZSH 上工作得很好,我可以输入 c <tab>
并且它完成就像我直接输入 cd $PROJECTS
一样。
在鱼上,我创建了一个 c.fish
这样的:
function c
cd $PROJECTS/$argv
end
complete --command c --no-files --arguments='(find $PROJECTS -mindepth 1 -maxdepth 2)'
但是正如您所想象的,它不能像 ZSH 版本那样工作,因为完成不知道 $argv
,并且不会完成第一个文件夹。
有没有办法在 Fish 中做同样的事情?
我考虑过创建一个 abbr
,但我真的很喜欢我今天在 ZSH 上使用它的方式。
如果 c
应该像 cd
一样将 $PWD
设置为 $PROJECTS
,这是一种方法:
function c_complete
# get the argument to 'c'
set arg (commandline -ct)
# save our PWD
set saved_pwd $PWD
# cd to $PROJECTS (and then back after)
# while in $PROJECTS, complete as if we are 'cd'
builtin cd $PROJECTS
and complete -C"cd $arg"
builtin cd $saved_pwd
end
complete --command c --arguments '(c_complete)'