tags:

views:

142

answers:

3

I'm having a hard time figuring this out. I'm typing the following with the help of the AutoClose.vim plugin:

function trim() {|}

| is the position of my cursor. What I want to achieve is as soon as I hit enter the code should look like:

function trim() {
    |
}

Instead, what happens now is:

function trim() {
|}
+1  A: 

Try :set cindent. This won't do precisely what you ask, since pressing Enter once only adds one newline, not two, but you should get something like:

function trim() {
    |}

After you type the last line of your function, use Ctrl+D (in Insert mode) to "dedent" (opposite of indent) the } back to the left margin.

You may need to also change the cinkeys option to make sure that the autoindent reacts to the keys you want.

Greg Hewgill
I have it set, but doesn't do the magic.
goyo
Actually, `cindent` might be the option you're looking for. I'll change my answer.
Greg Hewgill
+1  A: 

Another useful vim setting is :set smartindent which is very similar to cindent but more general. Investigate both with :help smartindent and :help cindent

Andrew
+1  A: 

Why don't you modify Autoclose.vim to put your closing brace on the next line, if your cursor is at the end of the opening line then when you hit enter it will indent it.

Before:

function trim() {|
}

After <Enter>:

function trim() {
  |
}
Pierre-Antoine LaFayette