微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

解决方法

你有没有改变tabstop选项?

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

" tabs
set tabstop=4
set shiftwidth=4
set expandtab

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

当您使用制表符时,每个工具都会以不同的方式显示制表符,您最终会花时间设置应为制表符(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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐