49

Vimscript 是否允许多行字符串?

python 和 ruby​​ 命令允许以下格式::python << EOF

你能用字符串做类似的事情吗?

4

3 回答 3

67

Vimscript 确实允许通过以反斜杠开始下一行来延续上一行,但这不如在 Ruby、PHP 或 Bash 中找到的 heredoc 字符串那么方便。

let g:myLongString='A string
\ that has a lot of lines
\ each beginning with a 
\ backslash to continue the previous one
       \ and whitespace before the backslash
       \ is ignored'

查看有关 line-continuation 的相关文档

于 2012-05-10T03:04:08.447 回答
14

Vimscript 多行字符串,点运算符:

枚举分配并包括以前的分配让您可以跨行连接

let foo = "bar" 
let foo = foo . 123 
echom foo                      "prints: bar123 

使用复合字符串连接运算符点等于:

let foo = "bar" 
let foo .= 123 
echom foo                      "prints: bar123

列出您的字符串和数字并加入它们:

let foo = ["I'm", 'bat', 'man', 11 ][0:4] 
echo join(foo)                                   "prints: I'm bat man 11 

同上,但加入数组切片

let foo = ["I'm", 'bat', 'man', [ "i'm", "a", "mario" ] ] 
echo join(foo[0:2]) . " " . join(foo[3]) 
"prints: I'm bat man i'm a mario

行首的反斜杠允许行继续

let foo = "I don't think mazer 
  \ intends for us to find 
  \ a diplomatic solution" 
echom foo 

let foo = 'Keep falling,  
  \ let the "grav hammer" and environment 
  \ do the work' 
echom foo 

印刷:

I don't think mazer intends for us to find a diplomatic solution
Keep falling, let the "grav hammer" and environment do the work

将您的秘密文本和最古老的书籍隐藏在一个函数中:

function! myblockcomment() 
    (*&   So we back in the club 
    //    Get that bodies rocking 
    !#@   from side to side, side side to side. 
    !@#$   =    %^&&*()+
endfunction 

自由格式文本的内存位置是它位于磁盘上的文件。该函数永远不会运行,否则解释器会呕吐,所以直到你使用 vim 反射来实现myblockcomment()然后做你想做的任何事情。除了让人眼花缭乱和混乱之外,不要这样做。

于 2018-07-07T14:51:31.253 回答
4

您不能使用<<创建字符串,但可以使用<<创建字符串列表。看:help :let=<<

下面是来自 vim doc 的示例

            let text =<< trim END
               if ok
                 echo 'done'
               endif
            END
于 2019-09-01T10:36:06.557 回答