Perform a non-regex search/replace in vim

RegexVimReplace

Regex Problem Overview


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

Regex Solutions


Solution 1 - Regex

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 - Regex

Use this option:

set nomagic

See :help /magic

Solution 3 - Regex

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 - Regex

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)

Solution 5 - Regex

Here’s how to disable regular expression search/replace only in command mode:

autocmd CmdWinEnter * set nomagic
autocmd CmdWinLeave * set magic

All plugins that depends on regular expression such as white-space remover should works as usual.

Solution 6 - Regex

Have you enabled magic?

:set magic

Solution 7 - Regex

Try the Edit Find and replace on the menu bar.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike CrittendenView Question on Stackoverflow
Solution 1 - RegexbjunixView Answer on Stackoverflow
Solution 2 - RegexMichael SpectorView Answer on Stackoverflow
Solution 3 - RegexHerbert SitzView Answer on Stackoverflow
Solution 4 - RegexChristian BrabandtView Answer on Stackoverflow
Solution 5 - RegexTaufik NurrohmanView Answer on Stackoverflow
Solution 6 - RegexfreitassView Answer on Stackoverflow
Solution 7 - RegexmartsbradleyView Answer on Stackoverflow