Automatic closing brackets for Vim
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.
Comments
-
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 over 10 years(That implies that I'm using delimitMate for Vim 7.4, working out of the box).
-
krystah over 10 yearsOh, 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 over 10 yearsCan 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 over 10 yearsWell 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 over 10 yearsI 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 over 9 yearsRemember to close vim so that your plugins get loaded.
-
cnp over 9 yearsThis is actually not true, at least when editing JavaScript code. Each of these has its own issues.
-
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 over 9 yearsI 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 about 9 yearsClosepairs 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 about 9 yearsYou don't need that keybinding if you just set
let g:delimitMate_expand_cr = 2
-
ozooner about 9 yearsThanks! was struggling to make auto-closing work as well, removing set paste was the troublemaker
-
shivams over 7 yearsWow. This one is a nice addition to the list. Better than most other plugins :)
-
Chenxiong Qi about 7 yearswork like a charm in my Vim 8.0
-
Jivan over 6 yearsVery nice – was looking for exactly this for a long time
-
user2233949 about 6 yearsAfter 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 almost 6 yearsThis 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 almost 6 years@rossijonas, Tabbing should have been taken care by autoindent. Not required if you set ai or families
-
rossijonas almost 6 years@dlmeetei True!!
-
SOFe over 5 yearsIs it possible to avoid typing
)
twice when I type()
? The right arrow key is hard to reach on my keyboard. -
dlmeetei over 5 yearsYou 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 over 5 years@SOFe What I do is type
(
and pressCTRL-O
to start insert mode for just one command, and then usea
to move to the end of the matching)
. I'm not sure but there is probably a faster way to do this. -
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 about 5 years@SOFe You can avoid that problem with
inoremap <expr> ) strpart(getline('.'), col('.')-1, 1) == ")" ? "\<Right>" : ")"
-
dman almost 5 yearsThis works but breaks my JavaScript indentions for Polymer components. Any suggestions?
-
dlmeetei almost 5 years@dman, I can not see how they are related.
-
Samir Alajmovic over 4 yearsThis plugin breaks other functionality with floating windows and other auto-completion frameworks :(
-
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 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 almost 4 years@JoePerkins have you find the answer?
-
SmootQ over 3 yearsFor
{<CR>
, I personally useinoremap {<CR> {<CR>}<ESC>ko
, because it takes indentation into account. -
Arthur over 2 years@SmoothQ Your normal mode
O
doesn't autoindent? I am pretty certain mine does. -
BryanK almost 2 yearsThis 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 almost 2 years@BryanK, If you want to paste , You need to
set paste
or usepastetoggle
. Effective editing becomes a habit :)