Vim replacement with wildcard and special characters

Viewed 99

Is it possible to perform wildcard replacement on string that contains lots of special characters?

String to search for:

<value="&lt;level/&gt;abc123"/>

Replace with:

<value="&lt;level/&gt;"/>

I basically want to get rid of all the abc123 in whole document.

2 Answers

If you want to replace a specific part in a string with Vim, you can use the \zs and \ze tags, which will indicate the pattern to be replaced. To your example, it should be as follows:

:s#<value="&lt;level/&gt;\zsabc123\ze"/>##g

The separation with the # character is done so that Vim does not get confused with the slashes (/) in your string, and the final g indicated that you want that substitution applied to all occurrences of the abc123 string in the entire line.

Use Atalajaka's solution.

If you do not want to manually type in the long complicated string which you search for, a better way than wild card is to select the long string using the v command, press "ay to yank the long string into register a.

:%s#Ctrl+Ra##g

Ctrl+R means that you press and hold the Ctrl key, and press R.

Add \zs and \ze before and after abc123 respectively.

The command line will become

:%s#<value="&lt;level/&gt;\zsabc123\ze"/>##g

Press Enter, all the instances of abc123 will be gone.

Related