Conditional replace in vim

后端 未结 5 667
渐次进展
渐次进展 2021-01-05 01:10

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:

5条回答
  •  旧巷少年郎
    2021-01-05 01:21

    That's a case for :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.)

提交回复
热议问题