Tom
Tom

Reputation: 828

vim how to replace backslash and &

I am using vim and I want to replace
&\\ \hline to \\ \hline Can somebody teach me how to do this? %s/\&\\/\\/gc didn't work. this is for latex table.

Upvotes: 2

Views: 102

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172580

Inside the search pattern, you don't need to escape & (\& is a special atom for branches), but \ needs to be doubled. In the replacement part, a & would have to be escaped, just like the \ (yes, this is a bit complex, unfortunately). So, this would work:

:%s/&\\\\/\\\\/gc

If you want to assert the following \hline, too, it's simpler to end the matching with \ze (but still assert that the following part is there); this avoids having to duplicate the part that should be kept (or alternatively capture and re-insert it in the replacement):

:%s/&\ze\\\\ \\hline//gc

Upvotes: 5

Related Questions