Auto-indent spaces with C in vim?

zxcv picture zxcv · Sep 19, 2008 · Viewed 120.8k times · Source

I've been somewhat spoiled using Eclipse and java. I started using vim to do C coding in a linux environment, is there a way to have vim automatically do the proper spacing for blocks?

So after typing a { the next line will have 2 spaces indented in, and a return on that line will keep it at the same indentation, and a } will shift back 2 spaces?

Answer

davr picture davr · Sep 19, 2008

These two commands should do it:

:set autoindent
:set cindent

For bonus points put them in a file named .vimrc located in your home directory on linux