tags:

views:

828

answers:

2

I like cindent, smartindent and =% features in vim, which properly indents the code. But, I have one problem with this, it indents everything with 8 spaces, and if I have few nested ifs, it can be very long line like here, though having so many nested ifs in first place is another question.

  4 int main()
  5 {
  6         if(x)
  7         {
  8                 if(u)
  9                 {
 10                         if(y)
 11                         {
 12                         }
 13                 }
 14         }
 15 }

I tried to set ts=1 and still it doesnt work.

Is there any way to make default indentation level to 4 spaces while using these features?

Edit set sw=4 solved the problem. No wonder vim always surprises me :)

+4  A: 

I believe you're looking for shiftwidth, abbreviated sw.

Edit: a couple quotes from documentation:

  • shiftwidth: Number of spaces to use for each step of (auto)indent. Used for |'cindent'|, |>>|, |<<|, etc.

  • tabstop: Number of spaces that a in the file counts for.

  • expandtab: In Insert mode: Use the appropriate number of spaces to insert a . Spaces are used in indents with the '>' and '<' commands and when 'autoindent' is on.

  • smarttab: When on, a in front of a line inserts blanks according to 'shiftwidth'. 'tabstop' is used in other places.

Depending on your style, you may have to change more than one of these. Have a look at their help entries if you need more clarification!

Jefromi
how does sw works for >>, <<. I tried if(1<<3) and cout<<"var"; and it doesnt give any space, am i missing something ?
seg.server.fault
It's `>>` when used in normal (or visual) mode, not when typed in insert mode. It indents the current line.
Martinho Fernandes
Note that you can prefix `<<` and `>>` with numbers to indent multiple times, but only when using from Visual mode. In normal mode, the number specifies a number of lines to indent.
Jefromi
+1  A: 

Try setting shiftwidth (sw) to 4.

And, if you want to use spaces instead of tabs, set expandtab (et). Then you can change all those tabs to spaces with :retab.

Martinho Fernandes