使用空格缩进,但在Vim中仍使用首选移位宽度?

使用空格缩进,但在Vim中仍使用首选移位宽度?,vim,indentation,Vim,Indentation,我在Vim中使用空格缩进时遇到问题。许多项目的编码指南都规定代码应该使用空格而不是制表符缩进。很公平。但是,当我在使用他们的代码时,有没有办法仍然看到并使用我喜欢的移位宽度 例如,这段代码使用8个空格缩进: function foo() { return 'foo'; } 当我在Vim中打开它时,它是否会显示(并缩进以进行编辑)为(移位宽度为4): 但是,当我保存它时,它是这样保存的(8个空格缩进): 当您使用制表符缩进时,这种情况非常自然。只需调整您的移位宽度,瞧,您可以编辑

我在Vim中使用空格缩进时遇到问题。许多项目的编码指南都规定代码应该使用空格而不是制表符缩进。很公平。但是,当我在使用他们的代码时,有没有办法仍然看到并使用我喜欢的移位宽度

例如,这段代码使用8个空格缩进:

function foo() {
        return 'foo';
}
当我在Vim中打开它时,它是否会显示(并缩进以进行编辑)为(移位宽度为4):

但是,当我保存它时,它是这样保存的(8个空格缩进):


当您使用制表符缩进时,这种情况非常自然。只需调整您的
移位宽度
,瞧,您可以编辑您个人喜欢的任何缩进。但是,现在大多数项目似乎需要使用空格进行缩进。而且似乎不可能用我个人喜欢的shiftwidth编辑文件,但在使用空格时仍然符合项目的缩进准则。

请参阅vim帮助以获取
retab
。它有一个例子,可以满足你的要求

如果您没有与我相同的vim帮助文件:

                                                        *retab-example*
Example for using autocommands and ":retab" to edit a file which is stored
with tabstops at 8 but edited with tabstops set at 4.  Warning: white space
inside of strings can change!  Also see 'softtabstop' option. >

  :auto BufReadPost     *.xx    retab! 4
  :auto BufWritePre     *.xx    retab! 8
  :auto BufWritePost    *.xx    retab! 4
  :auto BufNewFile      *.xx    set ts=4

retab
对对齐一无所知,因此以这种方式使用时会损坏对齐。在提交钩子中使用制表符和扩展(我的意思是/usr/bin/expand from coreutils)应该更好
function foo() {
        // edited to add this comment
        return 'foo';
}
                                                        *retab-example*
Example for using autocommands and ":retab" to edit a file which is stored
with tabstops at 8 but edited with tabstops set at 4.  Warning: white space
inside of strings can change!  Also see 'softtabstop' option. >

  :auto BufReadPost     *.xx    retab! 4
  :auto BufWritePre     *.xx    retab! 8
  :auto BufWritePost    *.xx    retab! 4
  :auto BufNewFile      *.xx    set ts=4