vim中的制表符和空格

时间:2008-09-16 07:11:20

标签: vim vi

启用autoindent时,如何阻止vim用制表符替换空格?

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

6 个答案:

答案 0 :(得分:77)

最好不要使用制表符。

: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 :(得分:40)

  

我想要的是自动缩进线与上一行具有完全相同的缩进字符。

:help copyindent

  

'copyindent' 'ci' boolean(默认关闭);本地缓冲区; {Vi无此功能}

     

自动输入a时复制现有行缩进的结构   新队。通常,新缩进由一系列重建   选项卡后跟空格(除非'expandtab'启用,   在这种情况下,只使用空格)。启用此选项会使   新行复制用于缩进的任何字符   现有的。如果新缩进大于现有缩进   线,剩余空间以正常方式填充。

     

注意:设置'copyindent'时会重置'compatible'   另请参阅'preserveindent'

:help preserveindent

  

'preserveindent' 'pi' boolean(默认关闭);本地缓冲区; {Vi无此功能}

     

更改当前行的缩进时,保留尽可能多的缩进   缩进结构尽可能。通常缩进由a替换   系列标签后跟空格(根据需要除外'expandtab'除外)   启用,在这种情况下只使用空格)。启用此选项   表示缩进将保留尽可能多的现有字符   用于缩进,只根据需要添加其他制表符或空格。

     

注意:使用“&gt;&gt;”时多次产生的缩进是混合的   标签和空格。你可能不喜欢这个。
  注意:设置'preserveindent'时会重置'compatible'   另见'copyindent'
  使用:重新占用以清理空白区域。

答案 2 :(得分:40)

您可以将所有TAB转换为SPACE

:set et
:ret!

或将所有SPACE转换为TAB

:set et!
:ret!

答案 3 :(得分:25)

这是.vimrc的一部分:

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

这对我很有用,因为我绝对不想在我的源代码中使用标签。从你的问题看来,你确实希望在下一行保留两个标签和七个空格,我不确定是否有办法教vim以适应这种风格。

答案 4 :(得分:1)

Maybe the bottom of this can help you?

  

标准vi从字面上解释了tab键,但是有一些流行的vi派生的替代品更聪明,比如vim。要让vim将tab解释为``indent''命令而不是insert-a-tab命令,请执行以下操作:

set softtabstop=2

答案 5 :(得分:1)

如果要根据“ts”的设置用空格替换所有选项卡,可以使用:retab。它也可以反过来。

相关问题