禁用 vim 中的所有自动缩进
在 TeX 中,vim 通常会搞乱我的缩进。主要是当我处于一个显示的方程式中时,我认为该方程式应该如下所示:
\[
x=\frac{y}{z}
\]
其中 x
前面的空白是一个制表符。
当我开始输入方程式时,我首先输入 \[
和 \]
标记,然后返回它们之间,输入制表符,然后输入方程式的其余部分。
Vim 不会做任何错误,直到我必须使用包含大括号的内容(例如 \frac{}
)。当我键入结束 }
vim 时,vim 会自动将整行的缩进移至左侧,从而撤消我键入的选项卡。
这太烦人了,如何禁用它?
我的 .vimrc
包含:
"indentation
set smartindent
set autoindent
set tabstop=5
set shiftwidth=5
filetype indent on
In TeX vim usually screws up my indentation. Mainly when I'm in a displayed equation which I think should look like this:
\[
x=\frac{y}{z}
\]
where the whitespace infront of the x
is one tab.
When I start type the equation I type the \[
and \]
marks first and then go back between them, typing the tab and then the rest of the equation.
Vim doesn't do anything wrong until I have to use something that incorporates curly braces (\frac{}
for example). When I type the closing }
vim automatically shifts the indentation for the whole line to the left, which undoes my typed tab.
This is very anoying, how do I disable it?
my .vimrc
contains:
"indentation
set smartindent
set autoindent
set tabstop=5
set shiftwidth=5
filetype indent on
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的问题中似乎存在一些术语的混合。在 vim 中,术语 autoindent 指向一种特殊的缩进,它简单地遵循前一行的缩进级别(有时非常方便)。要删除它,请手动
设置 noautoindent
,或将其写入您的 _vimrc 中。还有另外两种自动缩进,
cindent
和smartindent
。同样,如果您想禁用它们,请使用set nocindent
和set nosmartindent
如果您查看帮助(
help autoindent
,...)都得到了很好的解释。您喜欢(或不喜欢)哪一种主要取决于您的编程风格和习惯。所以,尝试一下,看看你最喜欢哪一个。不幸的是,我不再使用 LaTeX 了,所以我不熟悉它的内部文件类型缩进规则。
There seem to be a little mix of terms in your question. In vim the term autoindent points to a special kind of indentation that simply follows the indent level of the previous line (which is quite handy sometimes). To remove it
set noautoindent
by hand, or write it in your _vimrc.There are two other automatic kinds of indentation,
cindent
andsmartindent
. Similarly, if you wish to disable them go withset nocindent
andset nosmartindent
If you look in help (
help autoindent
, ...) they are all quite nicely explained. Which one you prefer (or don't) is mostly determined by your programming style and habits. So, try them out and see which you like most.Unfortunatelly, I don't use LaTeX that much anymore, so I'm not familiar with its internal filetype indentation rules.
对于遇到类似问题的其他人,对我有用的解决方案是:
:verbose set indentexpr?
查找导致缩进的文件indentexpr
所在的位置更改(对我来说是setlocal indentexpr=GetTeXIndent()
)setlocal indentexpr&
以关闭indentexpr
这删除了方括号、圆括号和大括号中的所有取消缩进。
For anyone else having a similar problem, a solution that worked for me was:
:verbose set indentexpr?
to find what file was causing the de-indentationindentexpr
is changed (for me it wassetlocal indentexpr=GetTeXIndent()
)setlocal indentexpr&
to turnindentexpr
offThis removed all de-indenting from brackets, parentheses, and braces.
下面的命令最终阻止了 VIM 假装它知道如何为我缩进文件并且只做我明确告诉它的事情:
礼貌 https://vim.fandom.com/wiki/How_to_stop_auto_indenting
The following command finally stopped VIM from pretending it knows how to indent files for me and only do what I explicitly tell it:
Courtesy https://vim.fandom.com/wiki/How_to_stop_auto_indenting
删除行
set autoindent
和set smartindent
以删除所有 vim 自动缩进。Remove the lines
set autoindent
andset smartindent
to remove all vim autoindentation.如果您使用的是 vim-latex 插件,请设置此选项:
对于其他插件,如果您不想像上面的答案那样关闭 indentexpr,您可以找到设置 indentkeys 的位置并注释掉这些行。当您键入右大括号时,这应该会停止触发重新缩进。
If you are using the vim-latex plugin, set this option:
For other plugins, if you don't want to turn off indentexpr as in the above answers, you can find where indentkeys is set and comment out those lines. This should stop triggering re-indent when you type a closing brace.
这是我确定的解决方案:
;)
Here's the solution I settled on:
;)
我刚刚花了几个小时解决了 javascript 的缩进问题,得出的结论是不要从 vimrc 中删除
filetype indent on
!此设置提供了最佳效果多种文件类型的智能缩进。如果您得到不好的结果,则可能存在配置问题。
文件特定的缩进设置
因此,如果您像我一样,您可能在 vimrc 中设置了
filetype indent on
并且不知道它在做什么。所有这些设置所做的就是告诉 vim 查找具有特定于文件类型的缩进规则的文件。它看起来有几个地方,但可能只有两个你感兴趣。
$VIMRUNTIME/indent/
~/.vimrc/after/indent/
第一个地方保存了 vim 自带的默认缩进规则。如果您要在新安装的 vim 上设置
filetype indent on
,那么所有智能缩进都将来自于此。例如,当您打开一个名为index.html
的文件时,将从$VIMRUNTIME/indent/html.vim
获取规则。根据我的经验,这些默认规则非常好,但它们可能会被其他设置搞乱。
第二个位置(
after
目录)允许您添加将取代第一个位置的设置。这很好,因为您不必编辑默认文件即可自定义它们。缩进的风格
正如您所见,有几种不同的缩进选项,但它们并不能很好地结合在一起。来自 Vim wiki:
自动缩进
我在 vimrc 中使用
filetype indent on
和set autoindent
,因为它们可以很好地协同工作。其他的我没设置。智能缩进和辛丹特
indentexpr
运行
(vimfolder)\indent\\(indentscripts)
中找到的文件类型缩进脚本。文件类型的 vim 文档 中提到了它,以及刚刚提到的其他文件(而且,它是我遇到问题的原因):故障排除
某些流氓插件可能会更改您的缩进设置,这就是您得到糟糕结果的原因。幸运的是,
verbose
会告诉您哪个文件是最后更改相关选项的文件。您可能会得到这样的结果
:如果发生这种情况,您可以移动该文件,关闭然后打开 vim,然后查看它是否可以解决您的问题。
关闭 TeX 的缩进设置
也许默认设置不适合您,并且您想禁用 TeX 的缩进设置,但保留所有其他文件类型。您可以通过在
after
目录中的文件中将这些值设置为其默认值来轻松实现此目的。我对 Tex 或 LaTex 不太了解,但是当我创建一个扩展名为
.tex
的文件并运行:filetype
时,它的文件类型为plaintex< /代码>。假设这是正确的,您需要创建一个文件
~/.vim/after/indent/plaintex.vim
。在该文件中:每当您打开
.tex
文件时,这都会将所有这些值设置为其默认值。I just spent a few hours working through indentation pains with javascript, and the conclusion I came to is don't remove
filetype indent on
from your vimrc!This setting provides the best smart indentation for multiple file types. If you're getting bad results with this, there's likely a configuration issue at hand.
File Specific Indent Settings
So if you're like me, you probably had
filetype indent on
in your vimrc and had no idea what it was doing.All this setting does is tell vim to look for files with filetype-specific indent rules. There are a few places it looks, but there are probably only two that you'd be interested in.
$VIMRUNTIME/indent/
~/.vimrc/after/indent/
The first place holds the default indent rules that come with vim. If you were to set
filetype indent on
on a fresh vim installation, this is where all the smart indenting would come from. For example, when you open a file calledindex.html
in would get the rules from$VIMRUNTIME/indent/html.vim
.In my experience, these default rules are pretty darn good, but they can get messed up by other settings.
The second place (the
after
directory) allows you to add settings that will supercede those in the first place. This is nice because you don't have to edit the default files in order to customize them.Flavors of Indentation
There are a few different indentation options as you've seen, and they don't all play nice together. From the Vim wiki:
autoindent
I use
filetype indent on
andset autoindent
in my vimrc, since they work well together. I don't have the others set.smartindent & cindent
indentexpr
Runs filetype indent scripts found in
(vimfolder)\indent\\(indentscripts)
. It is mentioned in the vim documentation for filetype, alongside the others just mentioned (also, it was the cause of the problem I was having):Troubleshooting
There's a chance that some rogue plugin is changing your indent settings and that's why you're getting poor results. Luckily
verbose
will tell you which file was the last to change the option in question.You may get a result such as
If that happens, you can move that file, close and open vim, and see if it fixes your problem.
Turning Off Indent Settings for TeX
Maybe the defaults just aren't doing it for you, and you want to disable the indent settings for TeX, but leave all other file types alone. You can easily do so by setting these values to their defaults in a file in the
after
directory.I don't know much about Tex or LaTex, but when I created a file with the
.tex
extension and ran:filetype
it had the filetype asplaintex
. Assuming that this is correct, you'd want to create a file,~/.vim/after/indent/plaintex.vim
. In that file:This will set all these values to their defaults whenever you open a
.tex
file.