George B.
George B.

Reputation: 595

vim auto-pairs remove/umwrap pair of brackets

I am bit confused whether vim auto-pairs (https://github.com/jiangmiao/auto-pairs) can remove/unwrap in pairs. For example if you have (foo) is it possible to delete either the first or second parenthesis and both of them are removed at the same time? Docs say:

g:AutoPairsMapCh

Default : 1

Map <C-h> to delete brackets, quotes in pair

but I don't understand if this is what I am looking for. I tried it in both insert and normal mode (i.e. pressing <Control-h> while the cursor is on a bracket) and it doesn't seem to work unless there is another issue (e.g. the <Control-h> binding). I have also checked the variable echo g:AutoPairsMapCh and returns 1

Upvotes: 4

Views: 2068

Answers (1)

filbranden
filbranden

Reputation: 8898

Support for deleting auto-pairs is restricted to only work when there's nothing (other than whitespace) between them.

The code enabled by g:AutoPairsMapBS, for the backspace key, and g:AutoPairsMapCh, for the <C-h> sequence (often sent by backspace in some systems), both of them enabled by default, will only work when backspace (or <C-h>) is used in insert mode to delete the left bracket, when the next thing after the cursor is the right bracket (possibly with some whitespace in between.)

This feature is useful if you write the wrong set of brackets and want to correct it. For example, if you want to write foo(bar), but you write foo{, which auto-pair completes to foo{} (with the cursor in between the curly braces), you can simply use basckspace to erase both pairs, getting back to foo, and then you can type (bar to get to foo(bar).

It doesn't really work if you have the cursor at the b in "bar", even in insert mode, and press backspace or <C-h>. It will delete the (, but since the ) is not right after the cursor, it won't really delete it.

As such, this feature is most useful to correct small typos noticed right away, rather than to modify pairs on code that's been already around for a while. It's more so that if you get something wrong right now, you can correct it as you normally would, instead of going out of your way to have to remove the unwanted right bracket that auto-pairs inserted for you...

If you want a plug-in to help with brackets that are already on the text and can either insert them, remove them, replace them with other sets of brackets, and more, take a look at Tim Pope's excellent surround.vim, which defines powerful normal mode commands to manage pairs of parentheses, brackets, quotes, XML tags, and more.

Upvotes: 5

Related Questions