79

Is there any WORKING plugin for Vim 7.4 that would automatically close brackets ('{}', '[]', '()') and maybe insert some carriage returns (for '{}' at least)?

I know this question has been asked quite a few times but none of the answers either do not seem to work in vim 7.4 or the plugin has not been updated or something.

So far I've tried vim-smartinput, autoclose and delimitmate and NONE of these have managed to insert any closing characters even though I have enabled the plugins and they otherwise seem to be active. I don't know if there has been some changes in vim that somehow breaks these plugins. None of these plugins seem to be updated in a ~year at least. Any working solutions that would work with vim 7.4?

Dago
  • 1,349
  • 1
  • 11
  • 19
  • 1
    there are different "autoclose" plugins. tried this? https://github.com/Townk/vim-autoclose – Kent Jan 23 '14 at 19:45
  • 1
    If you want an alternative you may want to look at surround. Surround is a good plugin to have even if you don't use if for this case. https://github.com/tpope/vim-surround – Peter Rincker Jan 23 '14 at 19:46
  • 1
    Surround is a fantastic plugin (as expected by the Pope), but I wouldn't ditch a delimiter-plugin for this or the other way around. Use both :) – krystah Jan 23 '14 at 20:28
  • *All* the plugins you listed work in 7.4 just as well (or badly) as they did in 7.3. AFAIK, 7.4 didn't break any existing plugin. Also, you don't "enable" Vim plugins. I think you should detail how you installed them. – romainl Jan 23 '14 at 21:13
  • Looking at the dates for the plugins it seems like most of these plugins were written for vim 7.0 or something... :) I think there might be something breaking the "closing" behaviour in my vimrc since none of the plugins so far have worked... Romainl: All of the plugins seem to have an "enable" command which does not always default to enabled. – Dago Jan 24 '14 at 06:24
  • http://paste.dy.fi/0vX Here is my .vimrc if anybody has any ideas. I have pathogen, airline, autocomplpop, code_complete, nerdtree and taglist installed. – Dago Jan 24 '14 at 06:50

6 Answers6

237

For those of us, who want a vanilla vim:

inoremap " ""<left>
inoremap ' ''<left>
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
inoremap {<CR> {<CR>}<ESC>O
inoremap {;<CR> {<CR>};<ESC>O

This autocomplete in insert mode, provided set paste is not set. Keep it in the vimrc to avoid typing it every time and when we don't want the mapping, we need to escape it using ctrl + v before typing the mapped char like ( { etc.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
dlmeetei
  • 9,905
  • 3
  • 31
  • 38
  • 13
    work like a charm in my Vim 8.0 – Chenxiong Qi Feb 18 '17 at 05:56
  • After trying multiple plugins, this works just the way I want it to. (The plugins seemed to insert unwanted indentations) . This works perfectly though. Thanks for the info. – user2233949 Apr 18 '18 at 17:51
  • 6
    This one works pretty good, and by the way I've made a minor improvement that was relevant for me... On the last tho mappings I added to the end of each line, since what you want is to write code one tab (more) indented than the last one, after opening a code block. inoremap { {}O inoremap {; {};O – rossijonas Jun 13 '18 at 17:40
  • 6
    @rossijonas, Tabbing should have been taken care by autoindent. Not required if you set ai or families – dlmeetei Jun 14 '18 at 07:03
  • @dlmeetei True!! – rossijonas Jun 14 '18 at 11:00
  • Is it possible to avoid typing `)` twice when I type `()`? The right arrow key is hard to reach on my keyboard. – SOFe Nov 29 '18 at 07:46
  • You have to type this only one time when saving in the `vimrc`, other times, vim should do automatically with the mapping, That's the beauty – dlmeetei Nov 29 '18 at 08:35
  • 1
    @SOFe What I do is type `(` and press `CTRL-O` to start insert mode for just one command, and then use `a` to move to the end of the matching `)`. I'm not sure but there is probably a faster way to do this. – saud Dec 02 '18 at 07:50
  • 3
    @dlmeetei you might have got me wrong. I am not referring to editing the .vimrc. I am referring to the way it works in Eclipse, where when you type `(`, it inserts a `)` behind, but it overwrites the `)` when you type `)` again rather than inserting another `)`. – SOFe Dec 02 '18 at 08:08
  • 10
    @SOFe You can avoid that problem with `inoremap ) strpart(getline('.'), col('.')-1, 1) == ")" ? "\" : ")"` – JoePerkins Apr 04 '19 at 16:49
  • This works but breaks my JavaScript indentions for Polymer components. Any suggestions? – dman Apr 30 '19 at 15:49
  • @dman, I can not see how they are related. – dlmeetei May 02 '19 at 06:01
  • 1
    @SOFe, Simplest, You can additionally have another mapping `inoremap () ()` , If you type in closing `)` before timeout it will behave well – dlmeetei Oct 05 '19 at 09:56
  • 1
    @JoePerkins How do I configure automatic deletion of matching braces or quotes when one part is deleted in insert mode? Thanks – Nanashi No Gombe Jan 07 '20 at 12:36
  • @JoePerkins have you find the answer? – Andres Felipe Jun 29 '20 at 20:32
  • For `{` , I personally use `inoremap { {}ko` , because it takes indentation into account. – SmootQ Jan 22 '21 at 13:32
  • 1
    @SmoothQ Your normal mode `O` doesn't autoindent? I am pretty certain mine does. – Arthur Dec 24 '21 at 09:49
  • This works but has a frustrating side effect... If you paste a large block of code consisting of many lines then decide to change your mind - It takes one undo key press for every one of these delimiters that exists in the pasted code as opposed to a single key press otherwise. – BryanK Apr 27 '22 at 14:29
  • @BryanK, If you want to paste , You need to `set paste` or use `pastetoggle`. Effective editing becomes a habit :) – dlmeetei Apr 28 '22 at 07:04
43

I've just tested ClosePairs, AutoClose, DelimitMate and AutoClose in Vim 7.4.131 on Ubuntu, using Pathogen.

Not surprisingly at all, all of them work.

Your question should be rephrased since you are not searching for "an auto closing plugin that works in 7.4"… because all of them do. What you should ask is "Here is my setup, how come none of the "auto closing" plugins work for me?".

A question one could answer with: "It's a mess. Start by commenting out everything before line 95."

Also, Vim is a very stable environment: "not being actively worked" on doesn't mean "not worth trying". Quite the contrary, actually.

0xc0de
  • 8,028
  • 5
  • 49
  • 75
romainl
  • 186,200
  • 21
  • 280
  • 313
  • Well sorry about the bad question wording then. I tried trimming my .vimrc to this http://paste.dy.fi/PAj and tried all the plugins again but nope, no inserted characters from any of them. – Dago Jan 24 '14 at 14:18
  • Remember to close vim so that your plugins get loaded. – Dana Woodman Aug 06 '14 at 17:00
  • 1
    This is actually not true, at least when editing JavaScript code. Each of these has its own issues. – cnp Sep 03 '14 at 05:02
  • @ChristopherPappas, everything in my answer is demonstrably true *in the scope of the question.* All of those plugins work in the sense that they get recognized and sourced by Vim. That they do what they claim to do without bugs or side effect (in JS or or other filetypes) and actually help the OP in his daily work is an entirely different matter and totally out of scope. – romainl Sep 03 '14 at 06:39
  • 1
    Closepairs not working correctly on vim 7.3 on mac... shows a list of errors while starting vim and it inserts a line break between pairs. Tested autoclose (1.2) and it works. – User Jan 31 '15 at 15:29
37

Giving a vote for delimitMate.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
krystah
  • 3,587
  • 2
  • 25
  • 43
  • (That implies that I'm using delimitMate for Vim 7.4, working out of the box). – krystah Jan 23 '14 at 20:21
  • 12
    Oh, and a must-have keybind for me that you might want to check out: `inoremap { {}O`. What it does: When you insert a `{` and hit Enter, it inserts the closing bracket and places the cursor on a new line between the pair. – krystah Jan 23 '14 at 20:25
  • Can you give me an example of the configuration or something? I've tried delimitMate but I cannot get it to insert a single character anywhere. I've tried changing loaded_delimitMate, delimitMate_autoclose and calling :delimitMateSwitch (which says delimitMate enabled/disabled) but no matter what I get no inserted characters anywhere. – Dago Jan 24 '14 at 14:17
  • I don't have a single configuration-line for `delimitMate` in my .vimrc. It just, like I said, worked straight out of the box. You are probably best off troubleshooting your .vimrc, starting Vim with just the plugin and nothing else. If that works, build it up from there. Best of luck – krystah Jan 24 '14 at 17:20
  • 1
    You don't need that keybinding if you just set `let g:delimitMate_expand_cr = 2` – Tallboy Mar 25 '15 at 17:30
17

Adding a new candidate: there's a plugin called lexima. It was introduced in October 2014, so it's rather new. According to the author, it features high customizability and dot-repeatability. It can also be customized to auto-close def - end pairs as in ruby.

Here's the presentation by the author in the VimConf held in Japan, which compares different ways to achieve auto-closing functionalities.

Yosh
  • 2,512
  • 3
  • 24
  • 30
3

I figured out why none of the plugins seemed to work for me. It seems the problem is related to the mksession feature in vim, I was only trying out these plugins within a saved session and for some reason that seems to prevent the plugins from working for some unknown reason. The plugins seem to be installed and "working" fine (as in the commands from the plugin are found etc.). Without opening a saved session at least delimitMate started working for me.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dago
  • 1,349
  • 1
  • 11
  • 19
  • I had copied some key bindings like `inoremap { {}` but it was not working. Issue was the **set paste** option in my .vimrc. Removed it and it worked! – Jatin Kumar Oct 01 '14 at 05:38
  • Thanks! was struggling to make auto-closing work as well, removing set paste was the troublemaker – ozooner Apr 26 '15 at 19:52
2

xptemplate is a snippets plugin that includes an option for autocompleting brackets/braces/etc. Install it and set

let g:xptemplate_brace_complete = '([{'

in your .vimrc. They start on the same line but if you hit enter it positions them correctly.

Conner
  • 30,144
  • 8
  • 52
  • 73