vim – 在变量上使用substitute
发布时间:2020-12-15 09:08:32 所属栏目:安全 来源:网络整理
导读:我如何在vimscript中完成以下函数? fun! Foo() let l:bar = "Hello there,world!" # Perform a substitution on l:bar,changing "world" to "kitten"endfun 也就是说,如何对变量执行替换,而不是当前缓冲区。 我知道,为了替换缓冲区,我可以写 silent :%s
我如何在vimscript中完成以下函数?
fun! Foo() let l:bar = "Hello there,world!" # Perform a substitution on l:bar,changing "world" to "kitten" endfun 也就是说,如何对变量执行替换,而不是当前缓冲区。 我知道,为了替换缓冲区,我可以写 silent :%s/world/kitten/g 但是什么是等价的命令替换变量?
请参阅:help substitute of:help substitute()。
它是替代命令的对应物(参见:help:substitute)。 substitute({expr},{pat},{sub},{flags}) *substitute()* The result is a String,which is a copy of {expr},in which the first match of {pat} is replaced with {sub}. This works like the ":substitute" command (without any flags). But the matching with {pat} is always done like the 'magic' option is set and 'cpoptions' is empty (to make scripts portable). 'ignorecase' is still relevant. 'smartcase' is not used. See |string-match| for how {pat} is used. And a "~" in {sub} is not replaced with the previous {sub}. Note that some codes in {sub} have a special meaning |sub-replace-special|. For example,to replace something with "n" (two characters),use "\n" or 'n'. When {pat} does not match in {expr},{expr} is returned unmodified. When {flags} is "g",all matches of {pat} in {expr} are replaced. Otherwise {flags} should be "". Example: > :let &path = substitute(&path,",=[^,]*$","","") This removes the last component of the 'path' option. :echo substitute("testing",".*","U ","") results in "TESTING". 在你的例子我猜让l:bar = substitute(l:bar,“world”,“kitten”,“”)应该工作 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |