Automatic closing brackets for Vim

VimVim PluginBrackets

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

For those of us, who want a vanilla [vim][1]:

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.

[1]: https://www.vim.org/ "The best general-purpose editor"

Solution 2 - Vim

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.

Solution 3 - Vim

Giving a vote for [delimitMate][1] before angry mods close the question for being opinion-based.

[1]: https://github.com/Raimondi/delimitMate "delimitMate"

Solution 4 - Vim

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.

Solution 5 - Vim

I'm answering my own question because 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.

Solution 6 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDagoView Question on Stackoverflow
Solution 1 - VimdlmeeteiView Answer on Stackoverflow
Solution 2 - VimromainlView Answer on Stackoverflow
Solution 3 - VimkrystahView Answer on Stackoverflow
Solution 4 - VimYoshView Answer on Stackoverflow
Solution 5 - VimDagoView Answer on Stackoverflow
Solution 6 - VimConnerView Answer on Stackoverflow