Automatically insert appropriate bracket in Vim

I spend too much time on the fact that Vim does not handle closing brackets, as most IDEs do. Here is what I want:

Enter this:

if( whatever ) { <CR> 

and get the following:

 if( whatever ) { | } 

where <CR> means that you press ENTER and | is the cursor position. This is what Eclipse does. This is what Visual Studio does. And this is what I want Vim to do.

I saw several plugins, tried several, and none of them seemed to give me this behavior. Of course, I cannot be the first programmer to want this.

+62
vim autocomplete
Dec 23 '10 at 19:28
source share
17 answers

In VimL, โ€‹โ€‹you can display { do exactly what you want:

 inoremap { {<CR>}<Esc>ko 

depending on your auto-indent setting, you can add <BS> after <CR> .

For a more complete solution, I would advise you to take a look at the plugins of Luc Hermitte vim . They have never let me down so far.

+32
Dec 23 '10 at 20:45
source share

To get the closing parentheses on a new line and the cursor on the line between the two parentheses, follow the suggestion of the first comment in an article called Making Brackets and brackets make it easier .

+16
Dec 23 '10 at 19:39
source share

Using AutoClose with the following works correctly.

 inoremap {<CR> {<CR>}<Co>O 

This is true for my system, at least (Unix terminal on Mac OS X).

+9
Jul 05 '13 at 18:05
source share

Here is what I have in my vimrc:

 let s:pairs={ \'<': '>', \'{': '}', \'[': ']', \'(': ')', \'ยซ': 'ยป', \'โ€ž': '"', \'"': '"', \''': ''', \} call map(copy(s:pairs), 'extend(s:pairs, {v:val : v:key}, "keep")') function! InsertPair(left, ...) let rlist=reverse(map(split(a:left, '\zs'), 'get(s:pairs, v:val, v:val)')) let opts=get(a:000, 0, {}) let start = get(opts, 'start', '') let lmiddle = get(opts, 'lmiddle', '') let rmiddle = get(opts, 'rmiddle', '') let end = get(opts, 'end', '') let prefix = get(opts, 'prefix', '') let start.=prefix let rmiddle.=prefix let left=start.a:left.lmiddle let right=rmiddle.join(rlist, '').end let moves=repeat("\<Left>", len(split(right, '\zs'))) return left.right.moves endfunction noremap! <expr> ,f InsertPair('{') noremap! <expr> ,h InsertPair('[') noremap! <expr> ,s InsertPair('(') noremap! <expr> ,u InsertPair('<') 

And for some file types:

 inoremap {<CR> {<Co>o}<Co>O 

// I know that the InsertPair function is trivial, but it saves time, because with it I can determine the display of commands and normal mode with one command without having to write a lot of <Left> s.

+4
Dec 23 '10 at 21:48
source share

Solution for brackets, brackets and brackets with tab between them.

 " Automatically closing braces inoremap {<CR> {<CR>}<Esc>ko<tab> inoremap [<CR> [<CR>]<Esc>ko<tab> inoremap (<CR> (<CR>)<Esc>ko<tab> 

Result:

 function() { | } 
+4
Jul 27 '18 at 20:17
source share

As you will see in the wikia tooltip : there are many solutions for this recurring question (I even have mine ).

That is, if you limit yourself to double brackets. Here you are in the context of a control instruction. Thus, you will most likely find scanned systems that do not expect you to type ") {" when entering the if statement. The Vim shortcut is generally shorter than what I read in your question. There are many options here again, you will most likely find snipmate, and may be my C & C ++ suite .

+3
Dec 24 '10 at 12:02
source share

For those who are faced with this, like me, and have been looking for something more recently updated than AutoClose: delimitMate I have found to be not only the preferred solution for AutoClose, but also for active development. According to vim.org , AutoClose has not been updated since 2009.

+3
Jun 14 '12 at 12:15
source share

delimitMate has a setting for this.

+2
Dec 23 '10 at 20:31
source share

I tried different plugins, but I found the most accurate and easiest to use auto-pairs . It is really intuitive, and when you install it, you get what you expected out of the box.

+2
Mar 03 '15 at 8:37
source share

Place the following in the .vimrc file:

 inoremap { {}<ESC>ha 

Whenever you press { in insert mode, {} is generated and places the cursor in the right brace so that you can immediately type between them. By placing curly braces in a sequence, rather than on different lines, you can place the tabs before } manually. This way you will never have the wrong number of tabs in front of it.

Perhaps someone can figure out how to count the number of tabs on which the cursor is on, and then create an equal number of tabs before } in a new line.

+2
Sep 18 '15 at 9:48
source share

Vim 7.4.849 patch added a binding to move the cursor without restarting the undo sequence. After upgrading to> = 7.4.849, then something like this works fine.

 inoremap ( ()<CG>U<Left> 

Note that I grabbed this directly from the documentation included in the patch. The simplest solution for this feature.

+2
Jan 08 '16 at 23:22
source share

I always preferred something like what exalted text does, where it adds a closing bracket as the next character, so I added the following to my .vimrc:

 inoremap ( ()<ESC>hli 

which moves the cursor between two brackets.

+2
Jan 18 '16 at 0:57
source share

Install and use the Vim script AutoClose , as recommended in an article titled Automatically add closing characters .

+1
Dec 23 '10 at 19:31
source share

Just a note to @Bob.

Karl Guertin AutoClose has a function called `double brace ', meaning you can enter curly braces twice, as shown below.

 int func_name(void) {{ ==> Type `{' twice here. 

will result in:

 int func_name(void) { | ==> Cursor here. } 

You can then enter one tab to indent according to the `shiftwidth 'setting, and then type.

+1
Jan 14 '11 at 7:45
source share

You do not need a special plugin for this, but it is a two-step process.

First add the following to your .vimrc to eat the triggering character:

 " eat characters after abbreviation function! Eatchar(pat) let c = nr2char(getchar(0)) return (c =~ a:pat) ? '' : c endfunction 

and then add this abbreviation to your .vimrc :

 inoreabbr <silent> { { \<cr><space><space> \<cr><esc>0i}<esc>k$i<cr>=Eatchar('\m\s\<bar>\r')<cr> 

\ at the beginning of lines two and three is a line continuation character. However, you could do it all on one line, and I added it so that I can distribute the abbreviation to reflect the output you are looking for - so things are a little intuitive.

0
Aug 18 2018-12-12T00:
source share

My decision:

 inoremap <expr> <CR> InsertMapForEnter() function! InsertMapForEnter() if pumvisible() return "\<Cy>" elseif strcharpart(getline('.'),getpos('.')[2]-1,1) == '}' return "\<CR>\<Esc>O" elseif strcharpart(getline('.'),getpos('.')[2]-1,2) == '</' return "\<CR>\<Esc>O" else return "\<CR>" endif endfunction 

Explaination:

The above code first checks to see if you use Enter to confirm the completion of the code; if not, then Enter will indent {|} . In addition, it provides auto-indented HTML tags.

Examples:

 if( whatever ){|} 

press Enter and you will get

 if( whatever ) { | } 

This also works for an HTML file. See the following example.

 <html>|<html> 

press Enter and you will get

 <html> | </html> 
0
Mar 02 '19 at 9:40
source share
 inoremap ( ()<ESC>i inoremap " ""<ESC>i inoremap ' ''<ESC>i inoremap { {<Cr>}<Esc>O 
0
May 22 '19 at 6:05
source share



All Articles