如何在Vim中更改制表位后避免缩进错误?

ric*_*ick 3 vi vim

我曾经在Vim中有8个空格的标签.然后我改为4个空格,但是现在每当我在更改为4个空格之前我写的一些代码中添加一行时,它会给我一个缩进不匹配错误,即使所有内容都很好地排列.有什么方法可以避免这个问题吗?

Ale*_*lli 9

你做过:%retab......?


ste*_*anB 9

你有没有改变tabstop选项?

我使用4个空格(当我点击标签时填充空格,插入实际的标签页面ctrl-v tab).以下是.vimrc中与标签相关的设置:

" tabs
set tabstop=4
set shiftwidth=4
set expandtab
Run Code Online (Sandbox Code Playgroud)

当您使用空格填充制表符时,您将始终插入空格而不是制表符,并且您的代码将始终看起来相同.

当您使用制表符时,每个工具都会以不同的方式显示制表符,您最终会花时间设置应为制表符(8,4,3.5)显示多少空格,而不是进行高效工作.

或者选择其中一个(来自vim 7.1 help tabstop):

    Note: Setting 'tabstop' to any other value than 8 can make your file
    appear wrong in many places (e.g., when printing it).


    There are four main ways to use tabs in Vim:
    1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
       (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim
       will use a mix of tabs and spaces, but typing <Tab> and <BS> will
       behave like a tab appears every 4 (or 3) characters.
    2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
       'expandtab'.  This way you will always insert spaces.  The
       formatting will never be messed up when 'tabstop' is changed.
    3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
       |modeline| to set these values when editing the file again.  Only
       works when using Vim to edit the file.
    4. Always set 'tabstop' and 'shiftwidth' to the same value, and
       'noexpandtab'.  This should then work (for initial indents only)
       for any tabstop setting that people use.  It might be nice to have
       tabs after the first non-blank inserted as spaces if you do this
       though.  Otherwise aligned comments will be wrong when 'tabstop' is
       changed.
Run Code Online (Sandbox Code Playgroud)