puk
puk

Reputation: 16782

Vim autoindent does not work the way I expect after 'if', 'for', 'while'; how can I make it work the way I want?

I have auto indentation turned on in my .vimrc file set autoindent which moves to the previous indentation like so ( is the cursor position)

while (! skynet.selfAware()){
    DARPA.funding++;█
}

Pressing enter/carriage return will yield

while (! skynet.selfAware()){
    DARPA.funding++;
    █
}

But is there any way to make vim smart enough to realize that a new indentation is required such that starting from this

while (! skynet.selfAware()){█
}

pressing enter/carriage return will yield

while (! skynet.selfAware()){
    █
}

instead of

while (! skynet.selfAware()){
█
}

Upvotes: 4

Views: 944

Answers (2)

dash-tom-bang
dash-tom-bang

Reputation: 17853

You could try turning cindent on. There are a huge number of options available for controlling how it works, see :help cinoptions-values.

Upvotes: 0

derekerdmann
derekerdmann

Reputation: 18252

Instead of autoindent, use smartindent. It does exactly what you're looking for.

Upvotes: 6

Related Questions