Perform a non-regex search/replace in vim

16,066

Solution 1

For the :s command there is a shortcut to disable or force magic. To turn off magic use :sno like:

:sno/search_string/replace_string/g

Found here: http://vim.wikia.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic

Solution 2

Use this option:

set nomagic

See :help /magic

Solution 3

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

Solution 4

If you want to search literally, you can use the \V regex atom. This almost does what you want, except that you also need to escape the backslash. You could define your own search command, that would search literally. Something like this:

 :com! -nargs=1 Search :let @/='\V'.escape(<q-args>, '\/')| normal! n

And then use :Search /foobar/baz

For Substitute, you could then after a :Search command simply use

:%s//replace/g

since then Vim would implicitly pick up the last search item and use the for replacing.

(Just want to give you some ideas)

Share:
16,066

Related videos on Youtube

Mike Crittenden
Author by

Mike Crittenden

Web developer and Drupal nut in Greenville, SC. Want to hire me for Drupal work? Of course you do!

Updated on January 18, 2021

Comments

  • Mike Crittenden
    Mike Crittenden over 3 years

    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 is there an alternative command to accomplish this?

    As an example, if I want to replace < with &lt;, I'd like to just be able to type s/</&lt;/g instead of s/\</\&lt\;/g

    • Tomalak
      Tomalak almost 13 years
      You should not have to escape < in the first place. Other than that, I don't think that there is any way to make & work literally.
    • Kellen Stuart
      Kellen Stuart over 4 years
      You should consider accepting bjunix's answer. The accepted answer doesn't actually answer the question - it's more a criticism than an answer.
    • Mike Crittenden
      Mike Crittenden about 4 years
      @KolobCanyon thank you, I did that.
  • Tomalak
    Tomalak almost 13 years
    Does not help in this particular case. Might help generally, though. +1
  • sunnet
    sunnet almost 13 years
    @Tomalak -- Not sure why you're saying it doesn't help in this case. Setting to nomagic seems to work fine for me. Then issue command :s/</&lt;/g and it works as desired. Not a great solution, since you generally want magic on, I think, but it does work.
  • Tomalak
    Tomalak almost 13 years
    @Herbert I've tried with the \M switch, which is supposed to be the same as nomagic (but on a per-regex basis), but obviously nomagic changes how & behaves in the replacement, while \M does not. That's also why I thought making & literal would be difficult.
  • sunnet
    sunnet almost 13 years
    @Tomalak -- Not sure, problem may be that the replacement portion of the substitute command is not a pattern (i.e, not a reg-ex), rather just a string with some special characters, so the pre-regex magic/nomagic flags don't operate in it. (Although setting magic/nomagic via set command do, see :h sub-replace-string .)
  • Vitali
    Vitali about 7 years
    The much better answer for plain-text search/replace is below. :sno rather than :s disables regex.
  • gbr
    gbr about 7 years
    Great, thanks, this is the only answers the helps with replacing any text, including dos paths with backslashes. I can't believe that vim does not yet have a plain-text search feature, how can one pretend that you go escape all the backslashes (or manually make some strange command like this) before starting to make a substitution?? Most of the times it would take me less to replace each occurrence manually!!! And I love regular expressions, it's just that sometimes you HAVE to avoid them!
  • gbr
    gbr about 7 years
    "typing the extra backslashes"? Have you ever used copy and paste? When you have the text you want to search in the clipboard having to go escape its characters is a major annoyance! Yes, you can type some strange escape command but it's absurd that you have to go to these lengths instead of the program providing such a basic feature!! And I love regexes but sometimes they are not the right solution!
  • gbr
    gbr about 7 years
    Turning off magic still leaves special meaning to the "$" and "\" characters
  • gbr
    gbr about 7 years
    Turning off magic still leaves special meaning to the "$" and "\" characters
  • gbr
    gbr about 7 years
    That command just uses the "\V" switch, which leaves backslashes to be escaped
  • gbr
    gbr about 7 years
    It's better to mention that the replacement text interprets some characters specially as well, so most people will probably want to use :%s//\='replace'/g , as suggested in the accepted answer (as I'm at it, most of the times you'll probably want to add a 'c' flag as well - :%s//\='replace'/gc).
  • Kellen Stuart
    Kellen Stuart over 4 years
    It's very odd to me that regex is the default in VIM. It's way more common to perform a search & replace without regex. I end up using Ctrl + Shift + H because of this. I guess this is another reason I need to get good at regex
  • ma11hew28
    ma11hew28 about 4 years
    @KolobCanyon, what does Ctrl+Shift+H do?
  • Kellen Stuart
    Kellen Stuart about 4 years
    @ma11hew28 it's a shortcut for "Replace in Path"... sorry that's a Rider IDE shortcut, not a VIM shortcut
  • ma11hew28
    ma11hew28 about 4 years
    @KolobCanyon, OK. Cool. Thanks. :-)
  • drevicko
    drevicko almost 4 years
    @KolobCanyon Interesting that you don't use regex much --- I almost always use regexes in search/replace in vim L-:
  • drevicko
    drevicko almost 4 years
    @gbr you can use \V to make the "$" normal also: vimdoc.sourceforge.net/htmldoc/pattern.html#/magic
  • Atralb
    Atralb over 3 years
    @bjunix your answer is misleading and seems to imply that the replacement for :s to use a non-regex pattern is :%sno, while it is simply :sno. the % is the line range where the substitute will be performed and means the whole file. Please consider updating your answer by removing the %.
  • questionto42standswithUkraine
    questionto42standswithUkraine almost 2 years
    Gives me: Command :sno[magic] is not yet implemented (PRs are welcome!)