What does shiftwidth do in VIM editor

3

I read this about shiftwidth option on Google: To change the number of space characters inserted for indentation, use the 'shiftwidth' option.

Ok Ok, but what's that more exactly? How can I try this to see what really happens? I created a new C file then added an IF confition, but nothing happens during identation:

if (a > 0) { nothing happens here }

Thank you!

user223787

Posted 2013-05-12T11:06:15.093

Reputation: 33

1Vim comes with an extensive documentation, try :h shiftwidth. – romainl – 2013-05-12T11:19:24.210

Answers

3

Lets say you have set shiftwidth=2

Firstly, you need to make sure that the file that a .c extension. Either by using

$ vim foo.c

for a new file or saving the file with a .c extension.

Now type

if (a > 0) {

and press enter. The next line will automatically be indented.

if (a > 0) {
  foo();

when you put a matching '}', vim will indent it properly.

if (a > 0) {
  foo();
}

Read this documentation for more clarity.

draxxxeus

Posted 2013-05-12T11:06:15.093

Reputation: 166

1I tried it, but affter pressing enter at the curly brace, it puts the cursor to the beginning of the line. – user223787 – 2013-05-12T11:25:55.983

are you typing the closing curly brace before pressing enter? Is there a plugin that auto closes brackets enabled? – draxxxeus – 2013-05-12T11:31:14.493

yes. No plugins – user223787 – 2013-05-12T12:02:29.297

Don't type the closing }. It is interpreted as the end of the statement and hence vim is not auto-indenting. – draxxxeus – 2013-05-12T12:05:19.687

I dind't type the closing }. Anyway, thanks for your help. I don't know what does that option do, but I will add it to .vimrc :) – user223787 – 2013-05-12T12:22:11.497

shiftwidth says the number of columns to be indented when you do >> or << – draxxxeus – 2013-05-12T12:26:32.480

3

I can recommend this screencast by Drew Neil which will explain how tabstop, softtabstop, shiftwidth and expandtab work together.

ypid

Posted 2013-05-12T11:06:15.093

Reputation: 283