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

One can take advantage of the text objects that are built in into Vim (see :help text-objects). The desired edit can be stated as a sequence of the following three actions.

  1. Cut the text inside the square brackets:

     di[
    
  2. Select the (empty) square brackets:

     va[
    

    Alternatively, you can just select the character under the cursor and the one to the left of it, because the command from step 1 always puts the cursor on the closing bracket:

     vh
    
  3. Paste the cut text over the selected brackets:

     p
    

Altogether, it gives us the following sequence of Normal-mode commands:

di[va[p

or, when the alternative form of step 2 is used:

di[vhp

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.


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).

Tags:

Vim