Match closing parentheses in vim find and replace

In vim mode in normal mode, I can navigate to the closing parenthesis using%. Is there an equivalent to what I can use in the find and replace command? Assuming I have latex code that I want to remove italics from. In a simple scenario, something seems :%s/{\\it \(.*\)}/\1/gc

to do the job, but in practice it will stop at the first closing curly brace, which is likely to close something else, as I may have {\it some text in italics {\bf and some in bold also} ...possibly so many other commands with curly braces... and finally my closing curly brace:}

.

+3


source to share


1 answer


This may be what you mean by "write a small macro and use search", but you can use normal mode commands with a command normal

like:

:g|{\\it|normal /{\\it^M%mm%5x`mx

      



:g

It will be run in each line matched by the pattern {\it

, and for each match it launches the normal mode command /{\\it

to go to the right place in the line %

to go to the closing brace mm

to put a mark on the cover, %

to return to the opening, 5x

to remove open command, then `m to return to the mark, and x

- delete the closing.

What this won't handle is multiple openings {\it

on the same line. (But you could just run it again). Faster than learning Perl.

0


source







All Articles