禁用 vim 中的所有自动缩进

发布于 2024-11-29 05:32:11 字数 540 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

挽袖吟 2024-12-06 05:32:12

您的问题中似乎存在一些术语的混合。在 vim 中,术语 autoindent 指向一种特殊的缩进,它简单地遵循前一行的缩进级别(有时非常方便)。要删除它,请手动设置 noautoindent,或将其写入您的 _vimrc 中。

还有另外两种自动缩进,cindentsmartindent。同样,如果您想禁用它们,请使用 set nocindentset 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 and smartindent. Similarly, if you wish to disable them go with set nocindent and set 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.

何处潇湘 2024-12-06 05:32:12

对于遇到类似问题的其他人,对我有用的解决方案是:

  1. 使用 :verbose set indentexpr? 查找导致缩进的文件
  2. 查找 indentexpr 所在的位置更改(对我来说是 setlocal indentexpr=GetTeXIndent()
  3. 将该行更改为 setlocal indentexpr& 以关闭 indentexpr

这删除了方括号、圆括号和大括号中的所有取消缩进。

For anyone else having a similar problem, a solution that worked for me was:

  1. Use :verbose set indentexpr? to find what file was causing the de-indentation
  2. Find where indentexpr is changed (for me it was setlocal indentexpr=GetTeXIndent())
  3. Change that line to setlocal indentexpr& to turn indentexpr off

This removed all de-indenting from brackets, parentheses, and braces.

陪我终i 2024-12-06 05:32:12

下面的命令最终阻止了 VIM 假装它知道如何为我缩进文件并且只做我明确告诉它的事情:

:setl noai nocin nosi inde=

礼貌 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:

:setl noai nocin nosi inde=

Courtesy https://vim.fandom.com/wiki/How_to_stop_auto_indenting

心碎的声音 2024-12-06 05:32:12

删除行 set autoindentset smartindent 以删除所有 vim 自动缩进。

Remove the lines set autoindent and set smartindent to remove all vim autoindentation.

污味仙女 2024-12-06 05:32:12

如果您使用的是 vim-latex 插件,请设置此选项:

let g:tex_indent_brace=0

对于其他插件,如果您不想像上面的答案那样关闭 indentexpr,您可以找到设置 indentkeys 的位置并注释掉这些行。当您键入右大括号时,这应该会停止触发重新缩进。

:verbose set indentkeys?

If you are using the vim-latex plugin, set this option:

let g:tex_indent_brace=0

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.

:verbose set indentkeys?
青芜 2024-12-06 05:32:12

这是我确定的解决方案:

pico <myfilename>

;)

Here's the solution I settled on:

pico <myfilename>

;)

心凉怎暖 2024-12-06 05:32:11

我刚刚花了几个小时解决了 javascript 的缩进问题,得出的结论是不要从 vimrc 中删除 filetype indent on

此设置提供了最佳效果多种文件类型的智能缩进。如果您得到不好的结果,则可能存在配置问题。

文件特定的缩进设置

因此,如果您像我一样,您可能在 vimrc 中设置了 filetype indent on 并且不知道它在做什么。

所有这些设置所做的就是告诉 vim 查找具有特定于文件类型的缩进规则的文件。它看起来有几个地方,但可能只有两个你感兴趣。

  1. $VIMRUNTIME/indent/
  2. ~/.vimrc/after/indent/

第一个地方保存了 vim 自带的默认缩进规则。如果您要在新安装的 vim 上设置filetype indent on,那么所有智能缩进都将来自于此。例如,当您打开一个名为 index.html 的文件时,将从 $VIMRUNTIME/indent/html.vim 获取规则。

根据我的经验,这些默认规则非常好,但它们可能会被其他设置搞乱。

第二个位置(after 目录)允许您添加将取代第一个位置的设置。这很好,因为您不必编辑默认文件即可自定义它们。

缩进的风格

正如您所见,有几种不同的缩进选项,但它们并不能很好地结合在一起。来自 Vim wiki:

自动缩进

'autoindent' 只是在开始新行时复制上一行的缩进。它对于结构化文本文件很有用,或者当您想要手动控制大部分缩进而不受 Vim 干扰时。 “自动缩进”不会干扰其他缩进设置,某些基于文件类型的缩进脚本甚至会自动启用它。

我在 vimrc 中使用 filetype indent onset autoindent,因为它们可以很好地协同工作。其他的我没设置。

智能缩进和辛丹特

“smartindent”在某些情况下会自动插入一层额外的缩进,并且适用于类似 C 的文件。 'cindent' 更可定制,但在语法方面也更严格。
“smartindent”和“cindent”可能会干扰基于文件类型的缩进,因此切勿与其一起使用。

当涉及到 C 和 C++ 时,基于文件类型的缩进会自动设置“cindent”,因此无需为此类文件手动设置“cindent”。在这些情况下,“cinwords”、“cinkeys”和“cinoptions”选项仍然适用。

通常,只有当您对基于文件类型的缩进的工作方式不满意时,才应手动设置“smartindent”或“cindent”。

indentexpr

运行 (vimfolder)\indent\\(indentscripts) 中找到的文件类型缩进脚本。文件类型的 vim 文档 中提到了它,以及刚刚提到的其他文件(而且,它是我遇到问题的原因):

重置“autoindent”、“cindent”、“smartindent”和/或“indentexpr”以禁用打开文件中的缩进。

故障排除

某些流氓插件可能会更改您的缩进设置,这就是您得到糟糕结果的原因。幸运的是,verbose 会告诉您哪个文件是最后更改相关选项的文件。

:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?

您可能会得到这样的结果

indentexpr=SomeMessedUpValue
Last set from ~/.vim/bundle/some_plugin/indent/plaintex.vim

:如果发生这种情况,您可以移动该文件,关闭然后打开 vim,然后查看它是否可以解决您的问题。

关闭 TeX 的缩进设置

也许默认设置不适合您,并且您想禁用 TeX 的缩进设置,但保留所有其他文件类型。您可以通过在 after 目录中的文件中将这些值设置为其默认值来轻松实现此目的。

我对 Tex 或 LaTex 不太了解,但是当我创建一个扩展名为 .tex 的文件并运行 :filetype 时,它的文件类型为 plaintex< /代码>。假设这是正确的,您需要创建一个文件 ~/.vim/after/indent/plaintex.vim。在该文件中:

set autoindent&
set cindent&
set smartindent&
set indentexpr&

每当您打开 .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.

  1. $VIMRUNTIME/indent/
  2. ~/.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 called index.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

'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering. 'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.

I use filetype indent on and set autoindent in my vimrc, since they work well together. I don't have the others set.

smartindent & cindent

'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax.
'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.

When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.

Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.

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):

Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

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.

:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?

You may get a result such as

indentexpr=SomeMessedUpValue
Last set from ~/.vim/bundle/some_plugin/indent/plaintex.vim

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 as plaintex. Assuming that this is correct, you'd want to create a file, ~/.vim/after/indent/plaintex.vim. In that file:

set autoindent&
set cindent&
set smartindent&
set indentexpr&

This will set all these values to their defaults whenever you open a .tex file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文