vim中的条件替换 [英] Conditional replace in vim
本文介绍了vim中的条件替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 vim search-and-replace 将所有 " 替换为 ' ,反之亦然.有没有办法一步实现这一目标?我正在考虑这样的事情:
I'd like do use vim search-and-replace to replace all " with ' and vice-versa. Is there a way to achieve this in one step? I'm thinking of something like this:
:s/\("\|'\)/\1=="?':"/
\1=="?':"
部分当然可以在 vim 中使用.
Where of course the \1=="?':"
-part is something that works in vim.
提前致谢!
推荐答案
:help sub-replace-special
:
:s/["']/\=submatch(0) == '"' ? "'" : '"'/g
这匹配两个引号中的任何一个(以更简单的方式使用 [...]
),然后使用三元运算符将每个引号转换为相反的引号.(对于更复杂的情况,您可以使用字典查找.)
This matches any of the two quotes (in a simpler way with [...]
), and then uses the ternary operator to turn each quote into its opposite. (For more complex cases, you could use Dictionary lookups.)
这篇关于vim中的条件替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文