Automatic closing brackets for Vim

87,353

Solution 1

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 2

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.

Solution 3

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 4

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

Solution 5

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.

Share:
87,353
Dago
Author by

Dago

I'm an EE student from Finland.

Updated on July 05, 2022

Comments

  • Dago
    Dago almost 2 years

    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?

  • krystah
    krystah over 10 years
    (That implies that I'm using delimitMate for Vim 7.4, working out of the box).
  • krystah
    krystah over 10 years
    Oh, and a must-have keybind for me that you might want to check out: inoremap {<CR> {<CR>}<C-o>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.
  • Dago
    Dago over 10 years
    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
    Dago over 10 years
    Well sorry about the bad question wording then. I tried trimming my .vimrc to this paste.dy.fi/PAj and tried all the plugins again but nope, no inserted characters from any of them.
  • krystah
    krystah over 10 years
    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
  • Dana Woodman
    Dana Woodman over 9 years
    Remember to close vim so that your plugins get loaded.
  • cnp
    cnp over 9 years
    This is actually not true, at least when editing JavaScript code. Each of these has its own issues.
  • romainl
    romainl over 9 years
    @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.
  • Jatin Kumar
    Jatin Kumar over 9 years
    I had copied some key bindings like inoremap { {}<Left> but it was not working. Issue was the set paste option in my .vimrc. Removed it and it worked!
  • User
    User about 9 years
    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.
  • Tallboy
    Tallboy about 9 years
    You don't need that keybinding if you just set let g:delimitMate_expand_cr = 2
  • ozooner
    ozooner about 9 years
    Thanks! was struggling to make auto-closing work as well, removing set paste was the troublemaker
  • shivams
    shivams over 7 years
    Wow. This one is a nice addition to the list. Better than most other plugins :)
  • Chenxiong Qi
    Chenxiong Qi about 7 years
    work like a charm in my Vim 8.0
  • Jivan
    Jivan over 6 years
    Very nice – was looking for exactly this for a long time
  • user2233949
    user2233949 about 6 years
    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.
  • rossijonas
    rossijonas almost 6 years
    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 <TAB> 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 {<CR> {<CR>}<ESC>O<TAB> inoremap {;<CR> {<CR>};<ESC>O<TAB>
  • dlmeetei
    dlmeetei almost 6 years
    @rossijonas, Tabbing should have been taken care by autoindent. Not required if you set ai or families
  • rossijonas
    rossijonas almost 6 years
    @dlmeetei True!!
  • SOFe
    SOFe over 5 years
    Is it possible to avoid typing ) twice when I type ()? The right arrow key is hard to reach on my keyboard.
  • dlmeetei
    dlmeetei over 5 years
    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
  • RottenCandy
    RottenCandy over 5 years
    @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.
  • SOFe
    SOFe over 5 years
    @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 ).
  • JoePerkins
    JoePerkins about 5 years
    @SOFe You can avoid that problem with inoremap <expr> ) strpart(getline('.'), col('.')-1, 1) == ")" ? "\<Right>" : ")"
  • dman
    dman almost 5 years
    This works but breaks my JavaScript indentions for Polymer components. Any suggestions?
  • dlmeetei
    dlmeetei almost 5 years
    @dman, I can not see how they are related.
  • Samir Alajmovic
    Samir Alajmovic over 4 years
    This plugin breaks other functionality with floating windows and other auto-completion frameworks :(
  • dlmeetei
    dlmeetei over 4 years
    @SOFe, Simplest, You can additionally have another mapping inoremap () ()<left> , If you type in closing ) before timeout it will behave well
  • Nanashi No Gombe
    Nanashi No Gombe over 4 years
    @JoePerkins How do I configure automatic deletion of matching braces or quotes when one part is deleted in insert mode? Thanks
  • Andres Felipe
    Andres Felipe almost 4 years
    @JoePerkins have you find the answer?
  • SmootQ
    SmootQ over 3 years
    For {<CR> , I personally use inoremap {<CR> {<CR>}<ESC>ko , because it takes indentation into account.
  • Arthur
    Arthur over 2 years
    @SmoothQ Your normal mode O doesn't autoindent? I am pretty certain mine does.
  • BryanK
    BryanK almost 2 years
    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.
  • dlmeetei
    dlmeetei almost 2 years
    @BryanK, If you want to paste , You need to set paste or use pastetoggle. Effective editing becomes a habit :)