【问题标题】:Indenting with spaces but still use preferred shiftwidth in Vim?用空格缩进但仍然在 Vim 中使用首选的 shiftwidth?
【发布时间】:2011-10-08 20:30:25
【问题描述】:

我在 Vim 中使用空格进行缩进时遇到问题。许多项目的编码指南指定代码应该使用空格而不是制表符缩进。很公平。但是,当我使用他们的代码时,有什么方法可以仍然查看和使用我喜欢的移位宽度?

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

function foo() {
        return 'foo';
}

当我在 Vim 中打开它时,有什么方法可以显示(和缩进,用于编辑)如下(shiftwidth 为 4):

function foo() {
    // edited to add this comment
    return 'foo';
}

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

function foo() {
        // edited to add this comment
        return 'foo';
}

当您使用制表符进行缩进时,这很自然地发生。只需调整您的shiftwidth 和瞧,您可以编辑您个人喜欢的任何缩进。但是,如今大多数项目似乎都需要使用空格进行缩进。而且似乎不可能以我个人喜欢的 shiftwidth 编辑文件,但在使用空格时仍然符合项目的缩进指南。

【问题讨论】:

标签: vim indentation


【解决方案1】:

查看retab 的 vim 帮助。它有一个例子来做你所要求的。

如果你没有和我一样的 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 对对齐一无所知,因此在以这种方式使用时会破坏它。在提交挂钩中使用制表符和expand(我的意思是 /usr/bin/expand 来自 coreutils)应该会更好。
猜你喜欢
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多