问题
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