What Does Shiftwidth Do in Vim Editor

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!

1

3 Answers

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.

6

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

1

Another important use of shiftwidth is to provide proper indentation to the code. Lets suppose we are using a.yaml file in which we wish to indent few lines of code together.

we set the shiftwidth with

esc:set shiftwidth=2

Now select the that you wish to indent with key

shift + v

Now, to indent the code right we use

shift + >

And, To indent the code left we use

shift + <

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest