Vim 中 Tab 键 == 4 个空格并在大括号后自动缩进
如何制作 vi-Vim 从不使用制表符(将空格转换为制表符,不好!),使制表符键 == 4 个空格,并在大括号块后自动缩进代码,例如Emacs 可以吗?
另外,如何保存这些设置,这样我就不必再次输入它们?
我见过与此相关的其他问题,但它似乎总是与我想要的有点偏离。
How do I make vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like Emacs does?
Also, how do I save these settings so I never have to input them again?
I've seen other questions related to this, but it always seems to be a little off from what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
正如其他几个答案中所指出的,现在首选的方法不是使用 smartindent,而是使用以下内容(在您的
.vimrc
):In your [.vimrc:][1] file:
帮助文件需要一些时间来适应,但是你读得越多,Vim 就越好:
甚至更好,您可以将这些设置嵌入到源代码中以实现可移植性:
要查看当前设置:
正如 graywh 在评论中指出的那样, smartindent已被 cindent 取代,它“工作得更巧妙”,尽管仍然主要用于具有类似 C 语法的语言:
As has been pointed out in a couple of other answers, the preferred method now is NOT to use smartindent, but instead use the following (in your
.vimrc
):In your [.vimrc:][1] file:
The help files take a bit of time to get used to, but the more you read, the better Vim gets:
Even better, you can embed these settings in your source for portability:
To see your current settings:
As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:
相关,如果您打开一个同时使用制表符和空格的文件,假设您已经将
整个文件中的所有制表符和空格替换为
Related, if you open a file that uses both tabs and spaces, assuming you've got
You can replace all the tabs with spaces in the entire file with
获得特定于文件类型的缩进的最佳方法是在 vimrc 中使用
filetype plugin indent on
。 然后,您可以在 .vim/ftplugin/c.vim 中指定诸如set sw=4 sts=4 et
之类的内容,而不必为所有正在编辑的文件和其他非 C 文件设置全局变量类型语法也将正确缩进(甚至 lisp)。The best way to get filetype-specific indentation is to use
filetype plugin indent on
in your vimrc. Then you can specify things likeset sw=4 sts=4 et
in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).要在大多数文件中使用 4 个空格制表符,在 Makefile 中使用真正的 8 宽制表符,以及在包括 C/C++ 在内的各种文件中自动缩进,请将其放入您的
~/.vimrc
文件中:To have 4-space tabs in most files, real 8-wide tab char in Makefiles, and automatic indenting in various files including C/C++, put this in your
~/.vimrc
file:在许多 Linux 系统(例如 Ubuntu)上,默认情况下
.vimrc
文件并不存在,因此建议您先创建它。不要使用主目录中存在的
.viminfo
文件。 它用于不同的目的。步骤 1:转到主目录
cd ~
步骤 2:创建文件
vim .vimrc
步骤 3:添加上述配置
步骤 3:保存文件,按 < kbd>Shift + ZZ。
On many Linux systems, like Ubuntu, the
.vimrc
file doesn't exist by default, so it is recommended that you create it first.Don't use the
.viminfo
file that exist in the home directory. It is used for a different purpose.Step 1: Go to your home directory
cd ~
Step 2: Create the file
vim .vimrc
Step 3: Add the configuration stated above
Step 3: Save file, by pressing Shift + ZZ.
推荐的方法是使用基于文件类型的缩进,并且仅在不够时才使用 smartindent 和 cindent。
将以下内容添加到您的 .vimrc
希望它作为不同的答案有所帮助。
The recommended way is to use filetype based indentation and only use smartindent and cindent if that doesn't suffice.
Add the following to your .vimrc
Hope it helps as being a different answer.
编辑 ~/.vimrc
添加以下行:
edit your ~/.vimrc
add following lines :
来自 VIM wiki:
From the VIM wiki:
自动缩进基于当前语法模式。 我知道,如果您正在编辑 Foo.java,则输入
{
并按 Enter 会缩进以下行。至于选项卡,有两种设置。 在 Vim 中,输入一个冒号,然后输入“set tabstop=4”,这会将制表符设置为显示为四个空格。 再次点击冒号并输入“set Expandtab”,这将为制表符插入空格。
您可以将这些设置放在主目录的 .vimrc (或 Windows 上的 _vimrc)中,这样您只需输入一次。
The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a
{
and hitting Enter indents the following line.As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.
You can put these settings in a .vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.
首先,不要在 Vim 中使用 Tab 键进行手动缩进。 Vim 在插入模式下有一对命令用于手动增加或减少缩进量。 这些命令是 Ctrl-T 和 Ctrl-D。 这些命令观察
tabstop
、shiftwidth
和expandtab
的值,并保持空格和制表符的正确混合(制表符的最大数量,后跟任何制表符)必要的空格数)。其次,如果您使用自动缩进,这些手动缩进键就不必经常使用。
如果 Ctrl-T 而不是 Tab 困扰您,您可以重新映射它:
您还可以重新映射 Shift-Tab 来执行 Ctrl -D deindent:
这里 ^T 和 ^D 是文字控制字符,可以作为 Ctrl-VCtrl-T 插入。
完成此映射后,您仍然可以使用 Ctrl-VTab 在缓冲区中键入文字 Tab。 请注意,如果您这样做,即使
:set Expandtab
打开,您也会得到一个未展开的制表符。使用
:set smarttab
可以实现与
映射类似的效果,这也会导致行前面的退格键表现得智能。在
smarttab
模式下,当Tab不在行首使用时,它没有特殊含义。 这与我上面将 Tab 映射到 Ctrl-T 不同,因为在行中的任何位置(在插入模式下)使用 Ctrl-T 都会增加该行的缩进。其他有用的映射可能是:
现在我们可以做一些事情,比如选择一些行,然后点击 Tab 来缩进它们。 或者在一行上按两次 Tab (在命令模式下)以增加缩进。
如果您使用正确的缩进管理命令,那么一切都由三个参数控制:
shiftwidth
、tabstop
和expandtab
。shiftwidth
参数控制缩进大小; 如果您需要四个空格缩进,请使用:set shiftwidth=4
或缩写:set sw=4
。如果仅这样做,则将使用空格和制表符的混合来创建缩进,因为
noexpandtab
是默认值。 使用:set Expandtab
。 这会导致您在缓冲区中键入的制表符扩展为空格,并且 Vim 管理的缩进仅使用空格。当
expandtab
打开时,如果您通过所有正确的 Vim 机制管理缩进,则tabstop
的值变得无关紧要。 它控制选项卡在文件中出现时的显示方式。 如果您设置 tabstop=8 Expandtab
,然后使用 Ctrl-VTab 将硬制表符潜入文件中,它将产生与像往常一样,下一个基于 8 列的选项卡位置。Firstly, do not use the Tab key in Vim for manual indentation. Vim has a pair of commands in insert mode for manually increasing or decreasing the indentation amount. Those commands are Ctrl-T and Ctrl-D. These commands observe the values of
tabstop
,shiftwidth
andexpandtab
, and maintain the correct mixture of spaces and tabs (maximum number of tabs followed by any necessary number of spaces).Secondly, these manual indenting keys don't have to be used very much anyway if you use automatic indentation.
If Ctrl-T instead of Tab bothers you, you can remap it:
You can also remap Shift-Tab to do the Ctrl-D deindent:
Here ^T and ^D are literal control characters that can be inserted as Ctrl-VCtrl-T.
With this mapping in place, you can still type literal Tab into the buffer using Ctrl-VTab. Note that if you do this, even if
:set expandtab
is on, you get an unexpanded tab character.A similar effect to the
<Tab>
map is achieved using:set smarttab
, which also causes backspace at the front of a line to behave smart.In
smarttab
mode, when Tab is used not at the start of a line, it has no special meaning. That's different from my above mapping of Tab to Ctrl-T, because a Ctrl-T used anywhere in a line (in insert mode) will increase that line's indentation.Other useful mappings may be:
Now we can do things like select some lines, and hit Tab to indent them over. Or hit Tab twice on a line (in command mode) to increase its indentation.
If you use the proper indentation management commands, then everything is controlled by the three parameters:
shiftwidth
,tabstop
andexpandtab
.The
shiftwidth
parameter controls your indentation size; if you want four space indents, use:set shiftwidth=4
, or the abbreviation:set sw=4
.If only this is done, then indentation will be created using a mixture of spaces and tabs, because
noexpandtab
is the default. Use:set expandtab
. This causes tab characters which you type into the buffer to expand into spaces, and for Vim-managed indentation to use only spaces.When
expandtab
is on, and if you manage your indentation through all the proper Vim mechanisms, the value oftabstop
becomes irrelevant. It controls how tabs appear if they happen to occur in the file. If you haveset tabstop=8 expandtab
and then sneak a hard tab into the file using Ctrl-VTab, it will produce an alignment to the next 8-column-based tab position, as usual.毕竟,您可以编辑 .vimrc,然后添加 conf
或执行命令
Afterall, you could edit the .vimrc,then add the conf
Or exec the command
最简单的一个是在 vim 文件中或者简单地编辑 .vimrc
Simplest one will be in vim file or simply edit the .vimrc