自定义目录补全附加空格
我有以下目录结构:
/home/tichy/xxx/yyy/aaa
/home/tichy/xxx/yyy/aab
/home/tichy/xxx/yyy/aac
我想输入 cdw y
我想出的解决方案给出了我的以下内容:
cdw y
到目前为止我有以下代码:
_cdw () {
local cur prev dirs
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=($(compgen -d -- /home/tichy/xxx/${cur}|perl -pe 's{^/home/tichy/xxx/}{}'))
# no difference, a bit more logical:
dirs=$(compgen -o nospace -d /home/tichy/xxx/${cur}|perl -pe 's/{^/home/tichy/xxx/}{}')
COMPREPLY=($(compgen -d -W ${dir} ${cur}|perl -pe 's{^/home/tichy/xxx/}{}'))
return 0
}
complete -F _cdw cdw
cdw () {
cd /home/tichy/xxx/$@
}
有什么问题吗?在我看来,完成过程似乎已经完成,并且不需要更多的输入。
I have the following directory structure:
/home/tichy/xxx/yyy/aaa
/home/tichy/xxx/yyy/aab
/home/tichy/xxx/yyy/aac
I would like to enter cdw y<TAB> and get cdw yyy/<CURSOR> as a result, so I could add cdw yyy/a<TAB> and get cdw yyy/aa<CURSOR>
The solution I came up with gives me the following:
cdw y<TAB> => cdw yyy<SPACE><CURSOR>
Following code I have so far:
_cdw () {
local cur prev dirs
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=($(compgen -d -- /home/tichy/xxx/${cur}|perl -pe 's{^/home/tichy/xxx/}{}'))
# no difference, a bit more logical:
dirs=$(compgen -o nospace -d /home/tichy/xxx/${cur}|perl -pe 's/{^/home/tichy/xxx/}{}')
COMPREPLY=($(compgen -d -W ${dir} ${cur}|perl -pe 's{^/home/tichy/xxx/}{}'))
return 0
}
complete -F _cdw cdw
cdw () {
cd /home/tichy/xxx/$@
}
Any ideas what's wrong? It seems to me that the completion process seems to be finished and isn't expecting any more input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
到目前为止,我发现的最简单的解决方案是生成如下所示的补全:
并在返回之前添加此行
。每当补全可能填充到斜杠时,这都会设置 nospace 选项,以便用户最终得到:
而不是:
并且每当完成可能填充时,它都不会设置 nospace 选项,直到完整的文件名,以便用户最终得到:
而不是:
The simplest solution I've found so far is to generate completions that look like this:
and add this line just before returning
This sets the nospace option whenever the completion may fill in until the slash so that the user ends up with:
instead of:
and it doesn't set the nospace option whenever the completion may fill in until a full filename so that the user ends up with:
instead of:
如果我理解正确的话,你想 bash 自动完成一个目录名称,并且没有多余的空间? (这就是我到达此页面时正在寻找的内容)。
如果是这样,当您注册完成函数时,请使用“-o nospace”。
我不知道 nospace 是否可以在 compgen 上运行。
If I understand correctly, you want to bash-autocomplete a directory name, and not have the extra space? (That's what I was looking for when I got to this page).
If so, when you register the completion function, use "-o nospace".
I don't know if nospace works on compgen.
像这样的事情怎么样:(
我不确定你是否可以从这里调用你的 shell 函数,否则你可能需要稍微复制它。)
这也摆脱了你的 perl hack :-)
How about something like this:
(I'm not sure if you can call your shell function from here or not, otherwise you may have to duplicate it a bit.)
This also gets rid of your perl hack :-)
完成为此提供了一个解决方案,没有任何解决方法: /etc/bash_completion 中定义的 funciton _filedir :
然后,指定以下内容就足够了:
completion provides a solution for this without any workaround: funciton _filedir defined in /etc/bash_completion:
Then, specifying the following is enough: