Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you auto indent a block in vim

Tags:

vim

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() {
|}
like image 360
goyo Avatar asked Oct 14 '22 11:10

goyo


1 Answers

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.

like image 196
Greg Hewgill Avatar answered Oct 18 '22 12:10

Greg Hewgill