Vim 脚本:扩展字符串中的通配符

发布于 2024-08-12 10:11:28 字数 197 浏览 4 评论 0原文

有一个 vim 变量,其字符串值如下:

foo%:p:r:sbar

%:p:r:s 是 vim 通配符。

如何展开字符串变量中的所有通配符?

不幸的是,expand(my_variable) 没有帮助。 我需要指定任何其他参数或使用其他 vim 函数吗?

谢谢。

There is a vim variable with the string value like this:

foo%:p:r:sbar

%:p:r:s is a vim wildcard.

How can I expand all wildcards in the string variable?

Unfortunately expand(my_variable) doesn't help.
Do I need to specify any additional argument or use other vim function?

Thanks.

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

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

发布评论

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

评论(2

缱绻入梦 2024-08-19 10:11:28

尝试 :help Expand()

文档的这一部分似乎与您特别相关:

            Modifiers:
                    :p              expand to full path
                    :h              head (last path component removed)
                    :t              tail (last path component only)
                    :r              root (one extension removed)
                    :e              extension only

            Example: >
                    :let &tags = expand("%:p:h") . "/tags"
            Note that when expanding a string that starts with '%', '#' or
            '<', any following text is ignored.  This does NOT work: >
                    :let doesntwork = expand("%:h.bak")
            Use this: >
                    :let doeswork = expand("%:h") . ".bak"

看起来您的尾随(可能还有前导)字符串无法与 expand() 一起使用。

但这确实有效:

:echo "foo" . expand("%:p:r:s") . "bar"

您可能可以重新编写脚本,以便在与其他字符串组合之前扩展通配符。或者,您可以尝试拆分连接的字符串,展开通配符,然后重新连接。

Try :help expand()

This section of the docs seems especially relevant to you:

            Modifiers:
                    :p              expand to full path
                    :h              head (last path component removed)
                    :t              tail (last path component only)
                    :r              root (one extension removed)
                    :e              extension only

            Example: >
                    :let &tags = expand("%:p:h") . "/tags"
            Note that when expanding a string that starts with '%', '#' or
            '<', any following text is ignored.  This does NOT work: >
                    :let doesntwork = expand("%:h.bak")
            Use this: >
                    :let doeswork = expand("%:h") . ".bak"

It looks like your trailing (and possibly leading) strings won't work with expand().

This does work though:

:echo "foo" . expand("%:p:r:s") . "bar"

Possibly you can rework your script so wildcards are expanded before they are combined with other strings. Alternatively you could try to split the concatenated string, expand the wildcards, then re-concatenate.

风为裳 2024-08-19 10:11:28

怎么样
join(map(split(mystring, '\ze[<%#]'), 'expand(v:val)'), '')

What about
join(map(split(mystring, '\ze[<%#]'), 'expand(v:val)'), '')
?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文