开发者

How do you auto indent a block in vim

开发者 https://www.devze.com 2022-12-15 19:16 出处:网络
I\'m having a hard time figuring this out. I\'m typing the following with the help of the AutoClose.vim plugin:

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 开发者_StackOverflowwant to achieve is as soon as I hit enter the code should look like:

function trim() {
    |
}

Instead, what happens now is:

function trim() {
|}


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.


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() {
  |
}


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


setting these in your vimrc should fix the issue:

set ai
set smartindent
ino {<CR> {<CR>}<ESC>O

The last line, in fact, autocomplete your { and place you in proper place upon typing { followed Enter.

0

精彩评论

暂无评论...
验证码 换一张
取 消