How to set auto insert new line and indent in Vim when use the auto close in pair

余生颓废 提交于 2020-01-04 05:22:02

问题


I found a Vim auto pair plugin called vim-autoclose. It works great just like other IDEs. For example, when you type { it gives you {|} (the "|" represents the cursor). Then, if you press "Enter", the {|} becomes:

{

      |

}

This is what I need, but unfortunately it disables my Chinese input method application,so I have to abandon it.

I have tried other autoclose plugins, but none of them can auto close in this way. I found a way to make auto close work like the way above.

:autocmd FileType c,java,javascript ino <buffer> { <C-G>u{<CR>}<Esc>O<C-T>

But it has a problem with the shiftwidth. For example, if I :set shiftwidth=4 the new line between {} will have an 8 space indent. The indent in the new line doubles the shiftwidth.

My question is how to set the indent of the new line to the same space indent of shiftwidth?


回答1:


inoremap { {<CR><CR>}<C-o>k<C-o>S

seems to work.




回答2:


After setting set ai and set smartindent in vimrc

Use the following

inoremap {<CR> {<CR>}<ESC>O

should work. The last character is capital O, not zero.



来源:https://stackoverflow.com/questions/12111934/how-to-set-auto-insert-new-line-and-indent-in-vim-when-use-the-auto-close-in-pai

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!