编写自定义 bash 完成规则

发布于 2024-09-08 23:21:44 字数 1291 浏览 4 评论 0原文

我的目录充满了具有相同前缀的文件,我希望能够在 vim 中快速打开它们。例如,我可能有:

$ ls *
bar:
bar_10  bar_20  bar_30

foo:
foo_10  foo_20  foo_30

我想要的是能够位于这些目录之一并输入:

$ vim <TAB>

并且它自动完成为:

$ vim bar_

为了实现这一点,我很高兴每个目录都有一个名为“.completion”的文件,其中包含“bar_” ”在其中。

我遇到的问题是我想要以下行为

  * "vim <TAB>"  -->  "vim bar_"           // no space
  * "vim bar_1"  -->  "vim bar_10 "        // space

:是光标,因此如果文件匹配,则在末尾添加空格。如果我们要匹配前缀,请不要添加空格。

到目前为止我所拥有的最好的就是这种行为减去在末尾添加空格。我尝试了各种方法,但都无济于事。以下是我所拥有的:

_vim()
{
    local cur opts
    local -a toks

    cur="${COMP_WORDS[COMP_CWORD]}"

    if [ -f .completion ]; then
        opts=`cat .completion`

        if [[ ${opts} = ${cur} ]]; then
            toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
        else
            if [[ -z ${cur} ]]; then
                toks=( $(compgen -W "${opts}" -- ${cur}) )
            else
                toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
            fi
        fi
    else
        toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
    fi

    COMPREPLY=( "${toks[@]}" )
}

complete -F _vim -o nospace vim

任何关于如何让它在文件名完成后添加空格但不在前缀完成后添加空格的想法将不胜感激。

I have directories full of files with the same prefix, which I want to be able to quickly open in vim. For example, I might have:

$ ls *
bar:
bar_10  bar_20  bar_30

foo:
foo_10  foo_20  foo_30

What I want is to be able to be in one of these directories and type:

$ vim <TAB>

and it autocomplete to:

$ vim bar_

To achieve this I am happy to have a file per directory called ".completion" which has "bar_" in it.

The issue I have is I would like the following behaviour:

  * "vim <TAB>"  -->  "vim bar_"           // no space
  * "vim bar_1"  -->  "vim bar_10 "        // space

Where | is the cursor, so if a file matches, add the space on the end. If we're matching the prefix, don't add a space.

The best I have so far is this behaviour minus the adding a space at the end. I've tried all sorts of things, all to no avail. The following is what I have:

_vim()
{
    local cur opts
    local -a toks

    cur="${COMP_WORDS[COMP_CWORD]}"

    if [ -f .completion ]; then
        opts=`cat .completion`

        if [[ ${opts} = ${cur} ]]; then
            toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
        else
            if [[ -z ${cur} ]]; then
                toks=( $(compgen -W "${opts}" -- ${cur}) )
            else
                toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
            fi
        fi
    else
        toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
    fi

    COMPREPLY=( "${toks[@]}" )
}

complete -F _vim -o nospace vim

Any ideas on how I can get it to add the space after the file name completion, but not after the prefix completion would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

痞味浪人 2024-09-15 23:21:44

sed 添加的尾随空格被删除。试试这个:

saveIFS=$IFS
IFS=
\n'    # this will allow filenames with spaces (but not filenames with newlines)
toks=( $(compgen -f -- "${cur}" ))    # the -- protects against filenames that start with a hyphen
toks=("${toks[@]/%/ }")    # add a trailing space to each element
IFS=$saveIFS

The trailing space that sed is adding is getting dropped. Try this:

saveIFS=$IFS
IFS=
\n'    # this will allow filenames with spaces (but not filenames with newlines)
toks=( $(compgen -f -- "${cur}" ))    # the -- protects against filenames that start with a hyphen
toks=("${toks[@]/%/ }")    # add a trailing space to each element
IFS=$saveIFS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文