1

I am using the AutoClose plugin in Vim. I would like to set the behaviour so that when I am in the state

while(i < N) {<cursor>}

then when (and only when) I press return, I get to:

while(i < N) {
    <cursor>
}

This is the behaviour in Sublime Text.

There is a similar question here: Automatically insert a matching brace in Vim but there the user is looking to start a new line as soon as the opening brace is created. Also it doesn't seem to work correctly for me.

If I use that, or do set cindent as suggested by @WoLpH the result is (as soon as I create the opening brace)

while(i < N) {
     <cursor>
    }

as in the second brace is indented and the cursor is indented by one space. My vimrc settings are:

filetype indent on
set ts=4
set sw=4
set et
Community
  • 1
  • 1
YXD
  • 31,741
  • 15
  • 75
  • 115

3 Answers3

0

There's a build-in feature for that, it's called cindent.

http://vim.wikia.com/wiki/Indenting_source_code#.27smartindent.27_and_.27cindent.27

So, just put this in your .vimrc and it should work:

set cindent
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • Not quite what I'm after - I'll update the question with what happens there – YXD May 30 '13 at 11:44
  • @MrE: yes, you need a few other flags as well to get it working properly. In my case the `}` automatically gets dedented after typing. – Wolph May 30 '13 at 12:09
0

The feature you are after is included in DelimitMate, another plugin similar to AutoClose.

For what it's worth, I've stopped using any autoclosing plugin a couple of months ago but I kept that "bracket expansion" feature with this line in my ~/.vimrc:

inoremap {<CR> {<CR>}<C-o>==<C-o>O
romainl
  • 186,200
  • 21
  • 280
  • 313
0

I edited the AutoClose script. Function OpenSpecial now contains:

return "\<esc>a\<CR>\<tab>;\<CR>".a:cchar."\<esc>\"_xk$\"_xa"

So I get my desired behaviour by typing {{l, good enough for now

YXD
  • 31,741
  • 15
  • 75
  • 115