Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically insert a matching brace in Vim

I spend way too much time fumbling around because Vim doesn't handle closing braces like most IDEs do. Here's what I want to happen:

Type this:

if( whatever ) { <CR> 

and get this:

if( whatever ) {   |   } 

where <CR> mean hit the ENTER key and | is the position of the cursor. This is what Eclipse does. It's what Visual Studio does. And it's what I want Vim to do.

I've seen a few plugins, tried a few, and none of them seem to give me this behavior. Surely I can't be the first programmer to want this.

like image 736
Bob Avatar asked Dec 23 '10 19:12

Bob


People also ask

How do I get matching brackets in vim?

You can use the % key to jump to a matching opening or closing parenthesis, square bracket or a curly brace.

How do I autocomplete in Vim?

to enter the *edit* mode and type any JavaScript keyword in the text editor and press *Ctrl + x* followed by *Ctrl + o*. Vim editor will show the possible auto-complete suggestions.


1 Answers

In VimL, you can map the { to do exactly as you wish:

inoremap { {<CR>}<Esc>ko 

depending on your autoindent setup, you may want to add a <BS> after <CR>.

For a more complete solution, I'd suggest you take a look at Luc Hermitte's vim plugins. They've never failed me so far.

like image 81
Michael Foukarakis Avatar answered Oct 01 '22 20:10

Michael Foukarakis