I’m having a problem using spaces for indenting in Vim. The coding guidelines of many projects specify that code should be indented using spaces and not tabs. Fair enough. But is there any way that, when I am working with their code, to still see and use my preferred shift width?
For example, this piece of code is indented using 8 spaces:
function foo() {
return 'foo';
}
Is there any way that, when I open this in Vim, it displays (and indents, for edits) as this (shiftwidth of 4):
function foo() {
// edited to add this comment
return 'foo';
}
But, when I save it, it saved like so (8 space indents):
function foo() {
// edited to add this comment
return 'foo';
}
This happens quite naturally when you use tabs for indenting. Just adjust your shiftwidth and voila, you can edit in whatever indent you personally prefer. But, most projects these days seem to require indents using spaces. And it seems impossible to edit a file in my personally preferred shiftwidth, yet still conform to a project’s indent guidelines when using spaces.
See the vim help for
retab. It has an example to do what you’re asking.In case you don’t have the same vim help file I do: