Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a non-regex search/replace in vim

Tags:

regex

replace

vim

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

like image 433
Mike Crittenden Avatar asked Jun 06 '11 16:06

Mike Crittenden


People also ask

How do I search and replace in Vim?

Basic Find and Replace In Vim, you can find and replace text using the :substitute ( :s ) command. To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the 'Esc' key.

How do you replace a word with another word in Vim?

Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.

Does replace use regex?

replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d.

How do I match a pattern in Vim?

In normal mode, press / to start a search, then type the pattern ( \<i\> ), then press Enter. If you have an example of the word you want to find on screen, you do not need to enter a search pattern. Simply move the cursor anywhere within the word, then press * to search for the next occurrence of that whole word.


2 Answers

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

like image 83
bjunix Avatar answered Sep 28 '22 19:09

bjunix


Use this option:

set nomagic 

See :help /magic

like image 33
Michael Spector Avatar answered Sep 28 '22 17:09

Michael Spector