Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-indent spaces with C in vim?

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?

like image 269
zxcv Avatar asked Sep 18 '08 22:09

zxcv


People also ask

How do I indent C in Vim?

First, go to the start point of codes to be formatted, then press v to start selection. Second, go to the end point. Third, press = to format the codes that have been selected. All braces and indentations will be aligned.

How do I indent 4 spaces in Vim?

The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4 , or the abbreviation :set sw=4 . If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab .

How do I set auto tab in Vim?

How to Turn On Auto Indent in Vim. To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing.


1 Answers

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

like image 77
davr Avatar answered Oct 16 '22 00:10

davr