user3446781
user3446781

Reputation:

How can I replace a string with special characters in vi?

How can you replace all occurrences of the string zUe*ES33Na with 8!fifHsgx in vi?

I tried using :%s/zUe*ES33Na/8!fifHsgx/g but it said substitute pattern match failed.

I have a feeling it is because there are 'special' characters that I'm trying to search for or replace with.

Basically is there a way to indicate to vi to interpret the characters as search characters and not as special characters?

This is for editing a file with passwords in it.

I got the same error when trying to replace the first string above with something simple like AAAAAA:

:%s/zUe*ES33Na/AAAAAA/g

so I have a feeling the * is causing an error.

Do I have to escape the * or can I somehow indicate to vi not to interpret it as containing special characters, by ,say, something like surrounding it with quotes?

Upvotes: 0

Views: 11586

Answers (2)

JackHasaKeyboard
JackHasaKeyboard

Reputation: 1685

Yes, it's the *.

When you search in Vi, you're searching a regular expression. This often looks like a regular string (%s/asdf/hjkl), but it's a regular expression that just happens to only match normal text. If you enter a special character that's used to denote something like * or . it will act accordingly.

What you have to do is escape it with a backslash and turn it into a normal character.

%s/zUe\*ES33Na/8!fifHsgx/g

Note that only the first part is affected by regex characters because replacing something with a regex makes no sense. The second part treats everything like a normal character.

Upvotes: 2

botika
botika

Reputation: 503

Use \ escape character

 :%s/zUe\*ES33Na/8!fifHsgx/g

Upvotes: 3

Related Questions