Perform a non-regex search/replace in vim

前端 未结 7 1696
暖寄归人
暖寄归人 2021-02-01 12:22

When doing search/replace in vim, I almost never need to use regex, so it\'s a pain to constantly be escaping everything, Is there a way to make it default to not using regex or

相关标签:
7条回答
  • 2021-02-01 12:57

    The problem is primarily caused by confusion about the role of the & in the replacement string. The replacement string is not a reg-ex, although it has some special characters, like &. You can read about role of & in replacement string here: :h sub-replace-special .

    I suspect the main problem for OP is not necessarily typing the extra backslashes, but rather remembering when a backslash is needed and when not. One workaround may be to start making use of "replacement expressions" when unsure. ( See :h sub-replace-expression.) This requires putting a `\=' in replacement string but for some people it may give you more natural control over what's being substituted, since putting a string literal in single quotes will give you the replacement string you want. For example, this substitute does what OP wants:

    :s/</\='&lt;'/g
    
    0 讨论(0)
提交回复
热议问题