Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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 
like image 350
Josh Avatar asked Jan 18 '10 06:01

Josh


People also ask

How do you jump between brackets in vim?

You can easily use the % key to jump to a matching opening or closing parenthesis, bracket or curly brace. You can also set the option showmatch . The cursor will briefly jump to the matching bracket, wen you insert one.

What do parentheses () and brackets [] In differ?

Generally, 'parentheses' refers to round brackets ( ) and 'brackets' to square brackets [ ]. However, we are more and more used to hearing these referred to simply as 'round brackets' or 'square brackets'. Usually we use square brackets - [ ] - for special purposes such as in technical manuals.

How do I select text between brackets in vim?

place the cursor on the opening parenthesis ( or braces { press esc key and press v to enter into the visual mode. now press the % symbol (this will select the whole text between parens inclusive) press the key y to yank (i.e. copy) the text (press d if you rather want to cut it.)

How do I match brackets in vi?

The % key can be used for the following : To jump to a matching opening or closing parenthesis, square bracket or a curly brace: ([{}])


2 Answers

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.

like image 182
archmage Avatar answered Sep 21 '22 20:09

archmage


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

like image 41
Justin Smith Avatar answered Sep 20 '22 20:09

Justin Smith