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...
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!
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.
I can recommend this screencast by Drew Neil which will explain how tabstop, softtabstop, shiftwidth and expandtab work together.
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 + <