如何在 Vim 中更改选项卡大小? [英] How do I change tab size in Vim?

查看:16
本文介绍了如何在 Vim 中更改选项卡大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我在 CSS 中添加选择器并按 Enter 来定义属性时,它最终都会像这样:

Every time I add a selector in CSS and I press Enter to define the properties it ends up like this:

#selector {
        property: value;
}

(8 格制表符)

如何配置 Vim 使其成为这样:

How can I configure Vim to make it like this:

#selector {
    property: value;
}

(4 格制表符)

推荐答案

扩展zoul的回答:

如果您想设置 Vim 在编辑特定文件类型时使用特定设置,您需要使用自动命令:

If you want to setup Vim to use specific settings when editing a particular filetype, you'll want to use autocommands:

autocmd Filetype css setlocal tabstop=4

这将使制表符显示为 4 个空格.设置 expandtab 会导致 Vim 在你按下 tab 时实际插入空格(空格的数量由 tabstop 控制);您可能希望使用 softtabstop 使退格正常工作(也就是说,在使用制表符时会发生这种情况时减少缩进,而不是每次删除一个字符).

This will make it so that tabs are displayed as 4 spaces. Setting expandtab will cause Vim to actually insert spaces (the number of them being controlled by tabstop) when you press tab; you might want to use softtabstop to make backspace work properly (that is, reduce indentation when that's what would happen should tabs be used, rather than always delete one char at a time).

要对如何设置做出全面的决定,您需要阅读有关 tabstopshiftwidthsofttabstopexpandtab.在 expandtab (:help 'expandtab) 下可以找到最有趣的部分:

To make a fully educated decision as to how to set things up, you'll need to read Vim docs on tabstop, shiftwidth, softtabstop and expandtab. The most interesting bit is found under expandtab (:help 'expandtab):

在 Vim 中使用制表符主要有四种方式:

There are four main ways to use tabs in Vim:

  1. 始终将 'tabstop' 设置为 8,将 'softtabstop' 和 'shiftwidth' 设置为 4(或 3 或任何您喜欢的值)并使用 'noexpandtab'.然后 Vim 将混合使用制表符和空格,但键入时会像每 4(或 3)个字符出现一个制表符一样.

  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 and will behave like a tab appears every 4 (or 3) characters.

将tabstop"和shiftwidth"设置为您喜欢的任何内容并使用expandtab".这样,您将始终插入空格.当 'tabstop' 改变时,格式永远不会混乱.

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.

将 'tabstop' 和 'shiftwidth' 设置为您喜欢的任何值并使用 |modeline|再次编辑文件时设置这些值.仅在使用 Vim 编辑文件时有效.

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.

始终将tabstop"和shiftwidth"设置为相同的值,以及noexpandtab".这应该适用于人们使用的任何制表位设置(仅适用于初始缩进).如果您这样做,在第一个非空白插入空格之后插入制表符可能会很好.否则当 'tabstop' 改变时对齐的注释会出错.

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.

这篇关于如何在 Vim 中更改选项卡大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆