While editing .scm
files it would be great if Vim would automatically put the ending brace )
as soon as I start (
. How do I do this?

- 2,845
- 6
- 47
- 67

- 52,135
- 87
- 247
- 375
11 Answers
You can map the opening brace to your liking:
:imap ( ()<left>

- 73,842
- 19
- 118
- 155
-
4The plugins mentioned in the other answers have the advantage of behaving "intelligently" when typing the corresponding closing ")". If this is not needed, then I'd also go with the straigt forward map. – user55400 May 20 '09 at 10:54
-
Some plugins also : - don't expand within comment and string contexts, - define a way to surround the selection with a pair of brackets, - inserts a placeholder/marker in order to jump past the closing bracket (which is consistent with most snippets/templates expander plugins). – Luc Hermitte May 20 '09 at 19:18
-
Is there a way to only do that at the end of a line? Sometimes it is more annoying than useful but with this, i would use it gladly... – alexpinho98 Jul 22 '13 at 12:46
Try to use AutoClose plugin.

- 57,943
- 15
- 89
- 102
-
8Not to be confused with the other, less aggressive (and less annoying, IMO), [AutoClose](http://www.vim.org/scripts/script.php?script_id=2009) by Thiago Alves. – Andrew Marshall Sep 26 '11 at 06:24
-
2@AndrewMarshall +1, your listed version is much less annoying. I came here looking for an alternative to the one in Mykola's answer because of it's quirks. – Dane O'Connor Oct 13 '11 at 03:18
-
FYI, This plugin seems to work fine but for me it introduced the "laggy" `
` behavior. So avoid if you care about this. – Steven Lu May 30 '13 at 20:36
The simplest answer is to include a map. Eg.:
:inoremap ( ()<left>
The problem is that you would need to add one entry for each symbol you want automatically closed ('{','[','"',etc). Besides, plugins are normally more smart, providing things like detecting the "closing" character and not repeating it.
The problem with the solution above and most plugins, is that they tend break the undo sequence (gundo anyone?), as explained in:help ins-special-special.
Here is list of plugins that do what you ask (from vimtips):
- delimitMate by Israel Chauca Fuentes (configurable, and doesn't break undo/redo/repeat, but - break iabbr) also on GitHub
- AutoClose by Karl Guertin (auto-closes specific hard-coded characters, but doesn't break undo/redo/repeat)
- AutoClose by Thiago Alves (configurable, but breaks undo/redo/repeat)
- auto-pairs Auto Pairs by Miao Jiang (configurable, but breaks undo/redo/repeat)
- ClosePairs by Edoardo Vacchi (configurable, but breaks undo/redo/repeat)
- smartinput by Kana Natsuno (configurable, but breaks undo/redo/repeat)
Besides vimtips, there is another nice explanation of the issue on the web.

- 1,120
- 12
- 17
-
2For anyone else who finds this in the future, I just started using auto-pairs, and unless I'm understanding the terminology wrong it didn't break my undo or history. Best one out of this list! It also handles deleting pairs on backspace! – Lethjakman Apr 27 '13 at 16:43
-
Which Vim version? Last time I checked there still wasn't a solution letting undo/redo/repeat work correctly with auto-closing in Vim 7.4 and onward. A few plugins had a hack using setline() that worked in earlier Vim versions. – Ben Jul 28 '14 at 14:17
I needed one too, and I already tried a few of the plug-ins:
AutoClose, http://www.vim.org/scripts/script.php?script_id=1849, is a bit aggressive.
simple pairs, http://www.vim.org/scripts/script.php?script_id=2339, depends on Python. If you are on Linux it is not a problem, but on Windows it can be a trouble to match the Vim version to the Python interpreter you have.
My advice would be ClosePairs, that you can find at http://www.vim.org/scripts/script.php?script_id=2373 which has been working perfectly for me. It is simple and useful.

- 576
- 5
- 11
There are many tips and plugins on the subject. Have a look at the relevant entry in the vimtips site.

- 31,979
- 7
- 69
- 83
I'm presently using auto-pairs and it works quite very well.
The issues of the the plugin breaking undo/redo/repeat seem to persist among some of the plugins listed above but i don't think its much of an issue (well, at least not to me at the moment).
Just one caveat though, i wasn't able to use :helptags to generate the help file with this plugin as at the time of writing this.

- 171
- 1
- 7
Check out this new plugin: vim-autoclose by Townk. The previously mentioned AutoClose was to aggressive, sometimes behaving in an undesirable way.

- 14,716
- 14
- 59
- 66
You can try downloading the following plugin AutoClose : Inserts matching bracket, paren, brace or quote
https://github.com/vim-scripts/Auto-Pairs
Tested this plugin for undu redo. 2013 It just works. Also with python-mode plugin.

- 141
- 1
- 10
There is a problem with using this (via the imap or one of the scripts). You won't be able to repeat the complete edit by using the .
command.
e.g. (foo) with . only gets you foo, without the brackets.
It works fine if you insert the brackets normally, as two characters.