gpt4 book ai didi

vim - Vim(脚本)-用作表达式时在字符串中加引号

转载 作者:行者123 更新时间:2023-12-04 17:48:43 31 4
gpt4 key购买 nike

我正在尝试执行以下操作:

:put = 'a string with "quotes"'

但我得到:
Missing quote:  'a string
Invalid expression: 'a string

我可以解决这个问题:
:let s:var = 'a "var"'
:put = s:var

通过阅读 :h :put,我发现 put(后面跟着 =)需要一个表达式。我不知道这是否是问题,我认为字符串 表达式(即值),就像许多其他语言一样。

请注意这也不起作用:
:put = "a string \"with quotes\""

最佳答案

可以在:help :put上找到了解这种特性的关键:

You need to escape the '|' and '"' characters to prevent them from terminating the command.



显然,未转义的 "将终止表达式(并可能开始注释)。因此,转义有效:
:put = 'a string with \"quotes\"'

您也可以使用(双引号)单引号来规避此问题:
:put ='a string with ''quotes'''

如果要使用双引号,它们都需要转义,而内部一次两次:
:put = \"a string with \\"quotes\\"\"

关于vim - Vim(脚本)-用作表达式时在字符串中加引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24164365/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com