我决定将标签大小从 4 更改为 2,因为为什么不呢?任何想查看代码的人都可以使用他们的偏好。
然而,一个问题。
如果我按 Tab 键,它会插入 2,但 Vim 的自动缩进仍然是 4。我要更改什么才能使 2?
另一个不相关的问题:哪种缩进样式最适合 C 和类似语言?我一直在使用 1TBS,但有很多可供选择。他们中的任何一个更专业或更受欢迎吗?
Pie*_*ien 44
Vim 主要使用 3 个设置作为缩进大小:
tabstop, ts: 当 Vim 在您打开的文件中遇到制表符时,它会将制表符显示为 {ts} 空格(参见制表符帮助,或:help tabstop在 Vim 中输入)。softtabstop, sts: 当你正在编辑一个文件并按下 Tab 键时,Vim 使用这个设置来定义插入的表格的宽度(参见softtabstop 帮助,或:help softtabstop在 Vim 中输入)。shiftwidth, sw: Vim 缩进时使用的空格数,使用自动缩进或常用的>>,<<命令。正如 Heptite 所注意到的,这就是您在这种特殊情况下要寻找的。并且最近版本的 Vim 确实允许您不定义此选项,shiftwidth然后将采用tabstop. 非常方便(请参阅shiftwidth 帮助)。例如,如果您使用以下设置:
set sts=4
set ts=2
set sw=8
Run Code Online (Sandbox Code Playgroud)
这些将产生以下行为:
tabstop设置为 2,这实际上相当于 2 个表格。这很容易检查,只需使用list和listchars选项来显示表格。>>,缩进将是 8 个空格宽(因此,相当于 4 个表格,基于tabstop值,同上)。
来自tabstop帮助(:help tabstop在 Vim 中):
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)
我个人主要使用第二种解决方案,带有 2 个空格宽的表格。
set ts=2
set sts=2
set et "expand tabs to spaces
Run Code Online (Sandbox Code Playgroud)
Hep*_*ite 20
尝试将“ shiftwidth”设置为与“ tabstop”相同的值。更好的是,如果您使用的是足够新的 Vim 版本,请将 ' shiftwidth'设置为 0,它将默认为 ' tabstop' 设置的任何值。
| 归档时间: |
|
| 查看次数: |
51437 次 |
| 最近记录: |