自定义目录补全附加空格

发布于 2024-12-21 15:08:39 字数 983 浏览 2 评论 0原文

我有以下目录结构:

/home/tichy/xxx/yyy/aaa
/home/tichy/xxx/yyy/aab
/home/tichy/xxx/yyy/aac

我想输入 cdw y并得到 cdw yyy/结果,所以我可以添加 >cdw yyy/a并得到cdw yyy/aa

我想出的解决方案给出了我的以下内容:
cdw y=> cdw yyy

到目前为止我有以下代码:

_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

春庭雪 2024-12-28 15:08:39

到目前为止,我发现的最简单的解决方案是生成如下所示的补全:

COMPREPLY=( $( compgen -W "file1 file2 file3 dir1/ dir2/ dir3/" ) )

并在返回之前添加此行

[[ $COMPREPLY == */ ]] && compopt -o nospace

。每当补全可能填充到斜杠时,这都会设置 nospace 选项,以便用户最终得到:

cmd dir1/<cursorhere>

而不是:

cmd dir1/ <cursorhere>

并且每当完成可能填充时,它都不会设置 nospace 选项,直到完整的文件名,以便用户最终得到:

cmd file1 <cursorhere>

而不是:

cmd file1<cursorhere>

The simplest solution I've found so far is to generate completions that look like this:

COMPREPLY=( $( compgen -W "file1 file2 file3 dir1/ dir2/ dir3/" ) )

and add this line just before returning

[[ $COMPREPLY == */ ]] && compopt -o nospace

This sets the nospace option whenever the completion may fill in until the slash so that the user ends up with:

cmd dir1/<cursorhere>

instead of:

cmd dir1/ <cursorhere>

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:

cmd file1 <cursorhere>

instead of:

cmd file1<cursorhere>
打小就很酷 2024-12-28 15:08:39

如果我理解正确的话,你想 bash 自动完成一个目录名称,并且没有多余的空间? (这就是我到达此页面时正在寻找的内容)。

如果是这样,当您注册完成函数时,请使用“-o nospace”。

complete -o nospace -F _cdw cdw

我不知道 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".

complete -o nospace -F _cdw cdw

I don't know if nospace works on compgen.

逆光下的微笑 2024-12-28 15:08:39

像这样的事情怎么样:(

COMPREPLY=( $(cdw; compgen -W "$(for d in ${cur}* ${cur}*/*; do [[ -d "$d" ]] && echo $d/; done)" -- ${cur}) )

我不确定你是否可以从这里调用你的 shell 函数,否则你可能需要稍微复制它。)

这也摆脱了你的 perl hack :-)

How about something like this:

COMPREPLY=( $(cdw; compgen -W "$(for d in ${cur}* ${cur}*/*; do [[ -d "$d" ]] && echo $d/; done)" -- ${cur}) )

(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 :-)

小猫一只 2024-12-28 15:08:39

完成为此提供了一个解决方案,没有任何解决方法: /etc/bash_completion 中定义的 funciton _filedir :

 626 # This function performs file and directory completion. It's better than
 627 # simply using 'compgen -f', because it honours spaces in filenames.
 628 # @param $1  If `-d', complete only on directories.  Otherwise filter/pick only
 629 #            completions with `.$1' and the uppercase version of it as file
 630 #            extension.
 631 #
 632 _filedir()

然后,指定以下内容就足够了:

_cdw () {
    local cur prev dirs
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    _filedir # add -d at the end to complete only dirs, no files
    return 0
}

completion provides a solution for this without any workaround: funciton _filedir defined in /etc/bash_completion:

 626 # This function performs file and directory completion. It's better than
 627 # simply using 'compgen -f', because it honours spaces in filenames.
 628 # @param $1  If `-d', complete only on directories.  Otherwise filter/pick only
 629 #            completions with `.$1' and the uppercase version of it as file
 630 #            extension.
 631 #
 632 _filedir()

Then, specifying the following is enough:

_cdw () {
    local cur prev dirs
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    _filedir # add -d at the end to complete only dirs, no files
    return 0
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文