I've been using the vim-surround plugin for as long as I can remember and have grown accustomed to typing <c-g>s ) when I want to insert text between brackets. I was writing with the atom editor recently and kind of liked the fact that the editor always inserts a closing bracket if you type an opening one. I realized that producing a similar effect in vim is trivial: I can just use the following mappings:

imap ( ()<c-o>i
imap [ []<c-o>i
imap { {}<c-o>i

This seemed so obvious that I started wondering why I haven't seen this done very often in people's vimrcs. Are there some possible conflicts / pitfalls related to this?

1 Answer 1


Well, for one it does not work very well with Repeating using .. That means, if you create a new buffer start insert mode and type (foobar your buffer will show (foobar). How redo the thing using .. It will redo only foobar.

I thought it would also break the undo sequence, but that does not seem to happen.

To fix the . repeating issue, I suggest to use:

inoremap ( ()<c-g>U<left>

That needs support for <C-G>U in insert mode which has been included in a late Vim 7.4 patch to work and has been requested a couple of years ago on vim_dev.

There might be other gotchas (or people in general just use a snippet/completion plugin instead) and do not care about the details :))

  • I'm intrigued as to how your fix works. Any chance of an explanation? (A pointer to where to look in the source would be fine: it doesn't seem self-evident to me that undo and . would share code.)
    – Rich
    May 10, 2018 at 9:24
  • @Rich Sorry, I did not follow, you want an explanation of what the patch did that implemented \<C-G>U? May 10, 2018 at 16:39

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.