vim – 在变量上使用substitute

前端之家收集整理的这篇文章主要介绍了vim – 在变量上使用substitute前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我如何在vimscript中完成以下函数
  1. fun! Foo()
  2. let l:bar = "Hello there,world!"
  3. # Perform a substitution on l:bar,changing "world" to "kitten"
  4. endfun

也就是说,如何对变量执行替换,而不是当前缓冲区。

我知道,为了替换缓冲区,我可以写

  1. silent :%s/world/kitten/g

但是什么是等价的命令替换变量?

请参阅:help substitute of:help substitute()。

它是替代命令的对应物(参见:help:substitute)。

  1. substitute({expr},{pat},{sub},{flags}) *substitute()*
  2.  
  3. The result is a String,which is a copy of {expr},in which
  4. the first match of {pat} is replaced with {sub}. This works
  5. like the ":substitute" command (without any flags). But the
  6. matching with {pat} is always done like the 'magic' option is
  7. set and 'cpoptions' is empty (to make scripts portable).
  8. 'ignorecase' is still relevant. 'smartcase' is not used.
  9. See |string-match| for how {pat} is used.
  10. And a "~" in {sub} is not replaced with the prevIoUs {sub}.
  11. Note that some codes in {sub} have a special meaning
  12. |sub-replace-special|. For example,to replace something with
  13. "\n" (two characters),use "\\\\n" or '\\n'.
  14. When {pat} does not match in {expr},{expr} is returned
  15. unmodified.
  16. When {flags} is "g",all matches of {pat} in {expr} are
  17. replaced. Otherwise {flags} should be "".
  18. Example: >
  19. :let &path = substitute(&path,",\\=[^,]*$","","")
  20. This removes the last component of the 'path' option.
  21. :echo substitute("testing",".*","\\U\\0","")
  22. results in "TESTING".

在你的例子我猜让l:bar = substitute(l:bar,“world”,“kitten”,“”)应该工作

猜你在找的Bash相关文章