Auto-indent spaces with C in vim?

CVimCoding StyleVi

C Problem Overview


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?

C Solutions


Solution 1 - C

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

Solution 2 - C

I http://tedlogan.com/techblog3.html">wrote all about tabs in vim, which gives a few interesting things you didn't ask about. To automatically indent braces, use:

:set cindent

To indent two spaces (instead of one tab of eight spaces, the vim default):

:set shiftwidth=2

To keep vim from converting eight spaces into tabs:

:set expandtab

If you ever want to change the indentation of a block of text, use < and >. I usually use this in conjunction with block-select mode (v, select a block of text, < or >).

(I'd try to talk you out of using two-space indentation, since I (and most other people) find it hard to read, but that's another discussion.)

Solution 3 - C

A lot of vim's features (like autoindent and cindent) are turned off by default. To really see what vim can do for you, you need a decent ~/.vimrc.

A good starter one is in $VIMRUNTIME/vimrc_example.vim. If you want to try it out, use

:source $VIMRUNTIME/vimrc_example.vim

when in vim.

I'd actually suggest just copying the contents to your ~/.vimrc as it's well commented, and a good place to start learning how to use vim. You can do this by

:e $VIMRUNTIME/vimrc_example.vim
:w! ~/.vimrc

This will overwrite your current ~/.vimrc, but if all you have in there is the indent settings Davr suggested, I wouldn't sweat it, as the example vimrc will take care of that for you as well. For a complete walkthrough of the example, and what it does for you, see :help vimrc-intro.

Solution 4 - C

Simply run:

user@host:~ $ echo set autoindent >> .vimrc

Solution 5 - C

I think the best answer is actually explained on the vim wikia:

http://vim.wikia.com/wiki/Indenting_source_code

Note that it advises against using "set autoindent." The best feature of all I find in this explanation is being able to set per-file settings, which is especially useful if you program in python and C++, for example, as you'd want 4 spaces for tabs in the former and 2 for spaces in the latter.

Solution 6 - C

and always remember this venerable explanation of Spaces + Tabs:

http://www.jwz.org/doc/tabs-vs-spaces.html

Solution 7 - C

Try:

set sw=2

set ts=2

set smartindent

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzxcvView Question on Stackoverflow
Solution 1 - CdavrView Answer on Stackoverflow
Solution 2 - CCommodore JaegerView Answer on Stackoverflow
Solution 3 - CrampionView Answer on Stackoverflow
Solution 4 - CJamesM-SiteGenView Answer on Stackoverflow
Solution 5 - Cuser809472View Answer on Stackoverflow
Solution 6 - Cmike511View Answer on Stackoverflow
Solution 7 - CCraig B.View Answer on Stackoverflow