How to quickly remove a pair of parentheses, brackets, or braces in Vim?

Viewed 34570

In Vim, if I have code such as (in Ruby):

anArray << [anElement]

and my cursor is on the first [, I can hop to ] with the % key, and I can delete all the content between the [] pair with d%, but what if I just want to delete the [ and ] leaving all the remaining content between the two. In other words, what's the quickest way to get to:

anArray << anElement
6 Answers

ma%x`ax (mark position in register a, go to matching paren, delete char, go to mark a, delete char).

EDIT:

%x``x does the same thing (thanks to @Alok for the tip).

Using the Surround plugin for Vim, you can eliminate surrounding delimiters with ds<delimeter>.

To install it via Vundle plugin, add

Plugin 'tpope/vim-surround' 

to your .vimrc file and run :PluginInstall.

The other answers work fine if you want to delete delimiters one line at a time.

If on the other hand you want to remove a function and it's delimiters from the entire file use:

:%s/function(\(.*\))/\1/g

which replaces function(arguments) with arguments everywhere in the file.

You can use d% while your cursor is on the bracket/parentheses.

Related