Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Vim Curly Braces, Square Braces, Parens act like Textmate

Tags:

vim

So I guess I'm not searching for the right thing but I'm looking to see how you can get VIM to act like Textmate when it comes to writing a set of curly braces, parens, or square brackets hit enter and you get this. Pipe indicates cursor.

      function doSomething(){
          |
      }

      #selector{
          |
      }

Instead of this garbage

      function doSomething(){
      |}

      #selector{
      |}

I already have the [{( closing each other when they are typed just the return and indentation is jacked. As usual any help would be appreciated.

like image 762
Chad Avatar asked May 20 '11 00:05

Chad


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 you make a curly brace?

On English keyboards, the open bracket and close bracket are on the same key as the [ and ] (square bracket) keys, located near the Enter key. To get a curly bracket, press and hold the Shift key, then press the { or } key.

What is the function of curly braces?

{} (curly braces)Define the beginning and end of functions blocks and statement blocks such as the for and if structures. Curly braces are also used for defining initial values in array declarations.

What does double curly braces mean?

Creating and executing a simple template Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.


2 Answers

I use the following mappings in my .vimrc:

inoremap {<cr> {<cr>}<c-o>O<tab>
inoremap [<cr> [<cr>]<c-o>O<tab>
inoremap (<cr> (<cr>)<c-o>O<tab>

So when I input:

function foo(){<cr>

I get:

function foo(){
    |
}

Similar with (<cr> and [<cr>.

like image 105
Randy Morris Avatar answered Nov 16 '22 00:11

Randy Morris


A note to delimitMate users

When using the delimitMate plugin, the mappings of the other answers interfere with the plugin. Luckily, this behavior is already available in delimitMate (but disabled by default), by setting the delmitMateExpansion options. E.g. add

let delimitMate_expand_cr = 1

to your .vimrc

like image 40
sebastian Avatar answered Nov 16 '22 00:11

sebastian