如何更改 Vim 中的制表符大小?
每次我在 CSS 中添加选择器并按 Enter
来定义属性时,它最终都会像这样:
#selector {
property: value;
}
(8-space tabs)
我如何配置 Vim 使其像这样:
#selector {
property: value;
}
(4-space标签)
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-space tabs)
How can I configure Vim to make it like this:
#selector {
property: value;
}
(4-space tabs)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这将插入四个空格而不是制表符。空格更“稳定”,这意味着用空格缩进的文本在浏览器和任何其他应用程序中显示相同。
This will insert four spaces instead of a tab character. Spaces are a bit more “stable”, meaning that text indented with spaces will show up the same in the browser and any other application.
要对一个会话进行更改,请使用以下命令:
要使更改永久生效,请将其添加到
~/.vimrc
或~/.vim/vimrc
:这将影响所有文件,不仅仅是 css。仅影响 css 文件:
如 Michał 的回答中所述。
To make the change for one session, use this command:
To make the change permanent, add it to
~/.vimrc
or~/.vim/vimrc
:This will affect all files, not just css. To only affect css files:
as stated in Michał's answer.
扩展zoul的答案:
如果你想设置Vim在编辑特定文件类型时使用特定设置,你需要使用自动命令:
这将使制表符显示为 4 个空格。设置
expandtab
会导致 Vim 在按下 Tab 时实际插入空格(空格数量由tabstop
控制);您可能希望使用softtabstop
使退格键正常工作(即,在使用制表符时减少缩进,而不是总是一次删除一个字符)。要就如何设置做出充分的决定,您需要阅读有关
tabstop
、shiftwidth
、softtabstop
和展开选项卡
。最有趣的部分位于expandtab
(:help 'expandtab
) 下:Expanding on zoul's answer:
If you want to setup Vim to use specific settings when editing a particular filetype, you'll want to use autocommands:
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 bytabstop
) when you press tab; you might want to usesofttabstop
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).To make a fully educated decision as to how to set things up, you'll need to read Vim docs on
tabstop
,shiftwidth
,softtabstop
andexpandtab
. The most interesting bit is found underexpandtab
(:help 'expandtab
):作为vim 的一行:
对于永久设置,请将这些行添加到~/.vimrc:
As a one-liner into vim:
For permanent setup, add these lines to ~/.vimrc:
此页面上的几个答案是针对所描述问题的“一次性”修复。这意味着,下次使用 vim 打开文档时,将返回之前的选项卡设置。
如果有人有兴趣永久更改选项卡设置:
添加以下行:(更多信息在这里)
<块引用>
然后保存文件并测试
Several of the answers on this page are 'single use' fixes to the described problem. Meaning, the next time you open a document with vim, the previous tab settings will return.
If anyone is interested in permanently changing the tab settings:
add the following lines: (more info here)
then save file and test
更新
如果您正在处理特定项目,我强烈建议使用editorconfig。
它允许您在存储库的根目录定义一个
.editorconfig
文件,定义要用于存储库中每种文件类型的缩进。例如:
有一个 vim 插件,可以根据您的文件的配置文件自动配置 vim打开。
除此之外,许多其他 IDE 和编辑器自动支持
.editorconfig
文件,因此它是不同环境的用户之间协作的最佳选择。原始答案
如果您需要经常更改大小并且不想将其绑定到特定文件类型,您可以在 .vimrc 文件上使用预定义命令来快速切换首选项:
这映射了两个不同的集键 \t 和 \m 的大小。您可以将其重新绑定到您想要的任何键。
UPDATE
If you are working in a particular project I highly recommend using editorconfig.
It lets you define an
.editorconfig
file at the root of your repository defining the indentation you want to use for each file type across your repository.For example:
There is a vim plugin that automatically configures vim according to the config file for file you open.
On top of that the
.editorconfig
file is automatically supported on many other IDEs and editors so it is the best option for collaborating between users with different environments.ORIGINAL ANSWER
If you need to change sizes often and you don't want to bind this to a specific file type you can have predefined commands on your .vimrc file to quickly switch preferences:
This maps two different sets of sizes to keys \t and \m. You can rebind this to whatever keys you want.
在 vim 命令模式下,写入:
其中 X 是新的所需空间长度
in vim command mode, write:
where X is your new desired space length
在我的
.vim/vimrc
(ubuntu bionic 下的 vim 8.0)中,我添加了如下行:
autocmd 文件类型 css setlocal tabstop=2
不起作用。
我创建了
.vim/indent
文件夹并添加了:css.vim
与它一起工作!
我尝试在另一台装有 ubuntu focus 和 vim 8.1 的计算机上运行,但不起作用!-(
in my
.vim/vimrc
(vim 8.0 under ubuntu bionic), I haveso added line like :
autocmd Filetype css setlocal tabstop=2
doesn't work.
I created
.vim/indent
folder and added in :css.vim
withand it works !
I tried to on another computer with ubuntu focal and vim 8.1, it doesn't work !-(