1

有一个带有字符串值的 vim 变量,如下所示:

foo%:p:r:sbar

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

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

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

谢谢。

4

2 回答 2

3

尝试: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"

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

于 2009-11-22T19:09:18.990 回答
2

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

于 2015-06-25T02:23:59.180 回答