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
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.
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.
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.)
The % key can be used for the following : To jump to a matching opening or closing parenthesis, square bracket or a curly brace: ([{}])
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With