Regular Expression
If you don't need to be able to support arbitrary levels of nesting, (if there's never a \cmd3
inside your \cmd2
), then you can do this by incorporating the inner brackets into your expression, and matching "any character that's not a bracket" [^}]
instead of "any character" .
:
:%s/\\cmd1{\([^{]*{[^}]*}\)}/(\1)/gc
Macro Alternative
Otherwise, you can use a macro instead:
You're using the c
confirm flag to inspect every change, so try the following:
/\\cmd1{<Cr>
: Find the first \cmd1
qq
: Start recording
dt{
: delete the \cmd1
%
: Jump to matching }
r)``r(
: Replace curly brackets with round ones
n
: Jump to next match
q
: Stop recording
Then press @q
to repeat the change, or n
to skip this instance and jump to the next. After you've used @q
at least once, you can use @@
as a faster shortcut.
If you actually don't want to review the changes as you make them, you can make the macro recursive by changing the recording steps 2–7 to:
qqqqqdt{%r)``r(n@qq
You can then invoke it a single time to make all the changes in one go: @q
cmds
? e.g.\cmd1{stuff \cmd2{other stuff \cmd3{even more stuff}}}
?\cmd1{stuff \cmd2{other stuff}} more stuff \cmd1{even more stuff \cmd2{yet other stuff}}