How do you find and replace curly braces { } in vim?

11,302

Solution 1

An extension to @chaos

The { character (ie: left brace, not to be confused with bracket [, or parentheses ( )
... does not need to be escaped.

You probably mean to remove all braces. The percent symbol should be before the 's', not after. It means to perform the search on all ranges.

So, just do:

:%s/{//g
:%s/}//g

All done!

You should consider reading up on VIM ranges. For example, to do replacements on the current line and up to 10 lines down, you could do:

:.,.+10s/}//g

Solution 2

You should put % before s to replace it in the whole file not only on current line:

:%s/{//g

Solution 3

:s/{//g works fine. Why in the world are you putting that % after the s? By doing that you're indicating % as your regex delimiter character, which is making the rest of your pattern not work because it's written as if / were your delimiter character.

Oh, I see, you mean :%s/{//g.

Share:
11,302
ZenLikeThat
Author by

ZenLikeThat

Updated on July 28, 2022

Comments

  • ZenLikeThat
    ZenLikeThat 2 months

    I tried :s%/{//g and :s%/\{//g. How do I find and replace (remove, actually) braces in vim?

    Thanks.

    EDIT: I meant to have the % before the s, so I may have just been mistyping. Thanks everyone for the swift replies.