在vim中使用制表符和空格

76

如何在开启自动缩进时,防止vim将空格替换为制表符?

例如:如果我在行的开头有两个制表符和7个空格,并且tabstop=3,然后我按Enter键,下一行的开头有四个制表符和1个空格,但我不想要这样...

6个回答

84

最好不要使用制表符。

:set expandtab

如果您想将文件中所有的制表符替换为3个空格(这看起来与tabstop=3非常相似):

:%s/^I/   /

(其中^I代表TAB字符)

来自VIM在线帮助:

'tabstop' 'ts'      number  (default 8)
        local to buffer
Number of spaces that a <Tab> in the file counts for.  Also see
|:retab| command, and 'softtabstop' option.

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.

1
值得注意的是,在粘贴模式下仍然会插入制表符。虽然这很合理,但我花了很长时间才弄清楚这些讨厌的制表符来自哪里。专业提示: :set list :set listchars=tab:>- - quartarian
FYI,Golang 使用制表符。 - Vitaly Zdanevich
如果只使用制表符进行缩进,生活会变得轻松许多。如果我曾经见过那个想出用空格而不是制表符的脑残白痴,我的生活会更好…… - lot

53
你可以将所有的TAB转换成SPACE
:set et
:ret!

或者将所有的SPACE转换为TAB

:set et!
:ret!

1
很高兴知道这个命令。我曾经通过使用gedit复制粘贴来解决我的“Python烦恼”。 - Nav

41

我希望自动缩进的行与前一行具有完全相同的缩进字符。

:help copyindent

'copyindent' 'ci' boolean (默认关闭);本地到缓冲区;{非 Vi 模式}

当自动缩进新行时,复制现有行缩进的结构。通常,新缩进是通过需要的一系列标签和空格重建的(除非启用 'expandtab',在这种情况下仅使用空格)。启用此选项使新行复制在现有行上用于缩进的任何字符。如果新缩进大于现有行上的缩进,则剩余的空间将以正常方式填充。

注意:当设置'compatible'时,'copyindent'将被重置。
另请参见'preserveindent'

:帮助保留缩进

'preserveindent' 'pi' boolean (默认关闭); 局部于缓冲区; {在Vi中不可用}

当改变当前行的缩进时,尽可能地保留缩进结构。通常情况下,缩进会被一系列制表符和所需的空格替换(除非启用了'expandtab',此时只使用空格)。启用此选项意味着缩进将尽可能地保留现有字符作为缩进,并仅在必要时添加额外的制表符或空格。

注意:使用“>>”多次后,结果的缩进是制表符和空格的混合。您可能不喜欢这个结果。
注意:当设置'compatible'时,'preserveindent'将被重置。
另请参见'copyindent'
使用:retab清理空白。


27

这是我的.vimrc的一部分:

set autoindent
set expandtab
set softtabstop=4
set shiftwidth=4

这对我来说效果很好,因为我绝对不想在我的源代码中有制表符。从你的问题中看来,你想在下一行保留两个制表符和七个空格,而我不确定是否有办法教会vim适应这种风格。


1
如果你想根据“ts”设置将所有制表符替换为空格,可以使用:retab。它也可以执行相反的操作。

3
Super retab”::retab 命令可以将所有制表符和空格序列转换为相应数目的制表符或空格,即使它们可能在“像这样的引用字符串中”。这个技巧展示了如何仅转换左边缘的缩进。第一个非白色字符之后的任何空格或制表符都不会受到影响。 - Aaron Thoma

1

也许这个链接的底部可以帮到你?

Standard vi interprets the tab key literally, but there are popular vi-derived alternatives that are smarter, like vim. To get vim to interpret tab as an ``indent'' command instead of an insert-a-tab command, do this:

set softtabstop=2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接