作者kornelius (c9s)
看板Editor
标题Re: [新手] tab的问题
时间Fri Feb 5 23:09:46 2010
基本上可以使用
set shiftwidth=4
来设定 tab 宽。
而使用
set expandtab
来将 tab 转成 space (实际存起来的档案将会是以 space , not tab )
如果要将既有的 <Tab> 转成 space 可以再 expandtab 之後输入:
:retab!
来做转换。
而 tabstop 是实际上读到档案的 \t (Tab 字元) 时,要解译为几个空白
通常设定为 4
set tabstop=4
另外 softtabstop 则是,你在编辑动作 (Backspace) 或是 (Tab) 时,对空白的解译
譬如说:
set noexpandtab
set sw=4
set tabstop=4
set softtabstop=4
新加入的 Tab 都会是 \t , Tab 为 4 个 space ,读取档案的 \t 会被显示为 4 个
space ,在操作 BS 的时候,4 个显示的空白会被当作一个 tab 删除。
实际上 softabstop 比较常用在 expandtab 的模式下:
set expandtab
set sw=4
set tabstop=4 " 怕档案里头仍有 \t
set softtabstop=4
如此被延展为空白的 space 也可以像是 tab 一样被删除固定的 space 数
(softtabstop=4)
附带一题,由於 tab 每个人的用法都不一样,而且变化很多
所以现在很多专案都只使用 space 而不使用 tab 了!
也就是:
set et sw=4 ts=4 sts=4
设定是被建议的。
当然如果你写 python 就不一样。 :p
这边顺便引入 vim help 的 'tabstop' 章节:
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.
※ 引述《shanmi (4/27视小中友之夜)》之铭言:
: 我在写fortran77
: 所以常常需要空个字元
: 我都按tab
: 我用geditor 发现他可以设定tab=空几格
: 所以是不是各家编辑器对tab的设定不同?
: 或者等到程式compile时也会把一个tab读成不同的空格数?
: 这样似乎要小心使用tab了
: 小弟刚开始写程式
: 感谢各位
--
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 113.61.198.68
※ 编辑: kornelius 来自: 113.61.198.68 (02/05 23:10)
※ 编辑: kornelius 来自: 113.61.198.68 (02/05 23:12)
1F:推 Uruzu:补个小技巧,设了expandtab後,想插入tab时,在insert mode 02/05 23:18
2F:→ Uruzu:下,先按ctrl+v 再输入tab就是真的tab了 02/05 23:19
※ 编辑: kornelius 来自: 113.61.198.68 (02/05 23:35)
3F:推 shanmi:太感谢了! 请问set shiftwidth是vi的指令吗? 02/08 16:42
4F:推 PCIT:是的 02/09 01:53
5F:推 zwai:强! 受益良多~ 02/09 22:39
6F:推 iwhiori:推!! 03/25 10:30
7F:推 darkgerm:推!! 08/09 18:40