How to delete enclosing braces in Vim?

Stack Overflow Asked by Robottinosino on December 19, 2020

I have the following text:

Monkeys eat {bananas}.

My cursor is in the middle of the word banana:

Monkeys eat {bana|nas}.

Here the | symbol denotes the cursor’s position.

How can I delete the braces from there?

I can change bananas to apples with a simple ci}apples, so perhaps I could use a similar trick just to get rid of the { and } characters?

Also, can I do this even in this case, which is actually what I really need to do?

networks {

(The simplified example above was just to introduce the concept.)

4 Answers

The task can be accomplished by means of Vim built-in text motions. Delete the text inside the braces, select the braces, and paste the previously deleted text over them:


Correct answer by ib. on December 19, 2020

What about this:


No plugin and simple.

Answered by kev on December 19, 2020

Delete the braces and leave everything else?



:help m - set a mark. In this case marking the initial cursor position so I can go back there at the end. :help [{ - moves the cursor to the opening brace of the smallest block enclosing the cursor. :help x - delete the brace which is now under the cursor. ]} and x - doing the same to the closing brace. And finally

help `

returning to marked position, the one called z created at the start.

"Plugins" aren't my style...

Answered by Alan Curry on December 19, 2020

Using Tim Pope's excellent surround.vim plugin (which I highly recommend), you would do ds{ for delete surrounding {

I understand that adding another plugin isn't always the ideal solution when you could find a native key sequence instead, but surround.vim is supremely useful, as it can also handle XML/HTML tags and perform enclosures on complex text objects. I regard it as one of those "stuck on a desert island, must have under any circumstance" plugins.

Answered by Michael Berkowski on December 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP