Last Updated: February 25, 2016
·
1.238K
· lwheng

Vim shortcut to remove brackets

Let's say in your source code you have something like:

1  let timesTwo = (2*)
2  let myList = map (timesTwo) [1,2,3]

Now, your compiler (or lint) might tell you that the round brackets in Line 2 is redundant (in this case it is!)

You wish to remove the brackets and you might use a combination of the d, x or w command in vim, or even go into INSERT mode and use backspace.

Here's my tip, add the following line to your .vimrc:

nmap <C-9> di(va(p

What that line does is that it maps the keyboard shortcut Ctrl+9 to execute the following di(va(p.

Now, go back to your source code and place your cursor on the ( in Line 2. Type Ctrl+9 and see the magic happen.

Further reading

  1. You can modify the mapping to another key combination.
  2. You change the ( to [ or { for different kinds of brackets
  3. You can break down the di(va(p command into di(, va( and p and explore what they do.

Thanks for reading!

2 Responses
Add your response

There is a plugin vim-surround could make it easier.

over 1 year ago ·

+1 for vim-surround

over 1 year ago ·