2

There are several plug-ins can accomplish this task, auto-add a closing bracket when we type: (, [ or {.

But then the cursor will be in the middle of paired bracket, when we finish inserting, we need to ESC, li to continue typing rest of the content.

This is quite frustrating, just autocomplete one character, we need to type 3 additional keys, is there a way to get around this? I still want to keep bracket auto-pairing though.

2
  • 5
    No. Stay in insert mode and press <Right> or (most auto-closing plugins let you do that) )or whatever the closing character is.
    – romainl
    Jun 8, 2016 at 11:13
  • 2
    Alternatively, you could try to create a mapping for (),{} (exact pair without anything in them) that will override the plugin functionality. But as @romainl said, the plugin should do this by default.
    – nobe4
    Jun 8, 2016 at 11:40

1 Answer 1

3

Fleshing out @nobe4's comment, you could do this:

inoremap <expr> ) matchstr(getline('.'), '\%' . col('.') . 'c.')==')'? '<Right>': ')'
inoremap <expr> ] matchstr(getline('.'), '\%' . col('.') . 'c.')==']'? '<Right>': ']'
inoremap <expr> } matchstr(getline('.'), '\%' . col('.') . 'c.')=='}'? '<Right>': '}'
  • <expr>: allows us to do a conditional mapping
  • matchstr(...): finds the character under the cursor

It will either insert the needed parin/bracket/brace, or pass over the one that's already there.

1
  • Thanks, that's the way I would go for. The easiest way to to type <Right> in Insert Mode, but obviously it is less efficient, since the hand need to left the main area to a corner. Moreover, for people who use HHKB pro, typing <Right> implies Fn +', which is also sub-optimal.
    – divinites
    Jun 10, 2016 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.