Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Instead of autoindent, use smartindent. It does exactly what you're looking for.
autoindent
smartindent
You could try turning cindent on. There are a huge number of options available for controlling how it works, see :help cinoptions-values.
cindent
:help cinoptions-values