92

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?

7 Answers 7

142

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

5
  • 13
    I would also recommend putting those into ~.vim/ftplugin/c.vim so that you're not using cindent all the time, but only for C/C++ files.
    – graywh
    Jan 4, 2009 at 21:05
  • @graywh: What about for pl, php, cpp, as, java, and all the other files I want to have the same indenting? Do I need to create a separate settings file for each one?
    – davr
    Sep 22, 2010 at 18:06
  • 18
    I rely Vim's bundled indent scripts (:filetype indent on) which is much better than just using 'cindent' always.
    – graywh
    Sep 24, 2010 at 19:08
  • 4
    why filetype indentation is better than using cindent is well explained here: vim.wikia.com/wiki/…
    – Jayen
    Mar 30, 2012 at 6:04
  • 1
    For autoindent, you might want to use the shorthand :set ai
    – Iam Zesh
    Jan 23, 2014 at 8:42
61

I 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.)

2
  • Very nice blog. I'm still fairly new to vim, nice to know there's good resources out there though.
    – zxcv
    Sep 18, 2008 at 22:51
  • 3
    You also missed changing softtabstop in addition to shiftwidth.
    – graywh
    Jan 4, 2009 at 21:01
8

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.

5

Simply run:

user@host:~ $ echo set autoindent >> .vimrc
1
  • 1
    I was not the one who down-voted, but it was probably because simply "set autoindent" does not on its own auto-indent upon typing "{" and "}", and nor does it automatically set the spacing to 2 spaces. This is what they asked for. Jan 14, 2013 at 12:44
5

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.

0

and always remember this venerable explanation of Spaces + Tabs:

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

2
  • 2
    What is with that guy's argument? I don't follow how not using the TAB character, and filling with hard-coded spaces instead, solves everyone's problems. That would make it impossible, for example, to be able to open a file and have the width of its indents appear according to your own preferences. May 4, 2009 at 1:58
  • 1
    This guy's solution is much better :) blogs.msdn.com/cyrusn/archive/2004/09/14/229474.aspx May 4, 2009 at 2:00
-1

Try:

set sw=2

set ts=2

set smartindent

3
  • According to the help, cindent is better than smartindent, but only works for C-like code. May 4, 2009 at 1:28
  • smartindent is deprecated (was only for C-like code, too). Filetype indent scripts are much better.
    – graywh
    Sep 24, 2010 at 19:10
  • @graywh besides the wiki which authoritative source states the deprecated status of smartindent?
    – memeplex
    Jul 22, 2015 at 10:01

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.