如果你使用单引号,你所需要的就是用两个单引号替换每一个单引号。
像这样的:
python import vim, re
python def senclose(str): return "'"+re.sub(re.compile("'"), "''", str)+"'"
python vim.command("let @r="+senclose("string with single 'quotes'"))
更新
:此方法在很大程度上依赖于
let abc='string
with newline'
和
execute "let abc='string\nwith newline'"
:当第一个失败时,第二个成功(这并不是在
:execute
以及普通文件)。另一方面,
eval()
在某种程度上更希望处理这个问题
string("string\nwith newline")
返回完全相同的内容
senclose
是的,所以我现在只使用
vim.eval
:
python senclose = lambda str: "'"+str.replace("'", "''")+"'"
python vim.eval("setreg('@r', {0})".format(senclose("string with single 'quotes'")))