Vim 中如何调用特定于编程语言的设置以及如何检测和覆盖它们?

How are programming language specific settings called in Vim and how to detect + overwrite them?

编辑器 Vim 带有针对许多不同编程语言的语法突出显示。
问题:

  1. 在 Emacs 中,特定于语言的设置称为“模式”。然而,在 Vim 中,术语“模式”指的是命令或插入模式。那么 Vim 编程语言特定设置的术语是什么?
  2. 文档的编程语言是根据其文件扩展名确定的,还是根据其内容确定的?
  3. 如何找出 Vim 处于哪种编程语言特定模式?
  4. 我怎样才能一劳永逸地覆盖某个 class 文档?

它们是如何命名的以及如何检测它们

Vim 使用文件 filetype.vim 来确定文件的“类型”。因此,如果您正在编辑 python 文件“example.py”并且您使用了命令 :set ft? 它应该显示 filetype=python。使用文件类型 VIM 确定是否加载与 python 相关的任何插件缩进规则或语法突出显示。

如何覆盖它们

您可以通过将这些设置放入 vimrc 来为编程语言编写自己的缩进规则、语法突出显示和其他规则。使用名为 VIML 的 vim 语言,您可以在 Learn Vimscript the Hard Way 中看到这个 section,作者在其中检测药水文件类型。一旦检测到使用文件扩展名完成的文件类型,例如 python 文件是 *.py 或 erlang 文件是 *.erl,您就可以添加自己的语言特定设置。

根据要求发布答案。

(1) In Emacs, language-specific settings are called "modes". In vim, however, the term "mode" refers to command vs insert mode. So what is the vim term for programming language specific settings?

等价的 Vim 项是 filetype。 Vim 使用 filetypes 来应用特定于语言的选项、缩进、语法突出显示、键映射等。这在帮助中有详细描述,请参阅 :h filetype

要启用自动 filetype 检测和处理,您通常会在 vimrc:

中添加类似的内容
filetype plugin indent on
syntax on

要覆盖 Vim 以这种方式提供的设置,您需要将覆盖添加到文件 ~/.vim/after/ftplugin/<filetype>.vim(或等效文件)。有关详细信息,请参阅 :h ftplugin

(2) Is the programming language of a document determined from its file name extension, or from its contents?

两种方法都用了。 Vim 在其运行时目录中的文件 filetype.vim 中进行大部分 filetype 检测。要找出此文件的确切位置:

:echo $VIMRUNTIME.'/filetype.vim'

插件可以添加更多 filetype,和/或覆盖标准插件的检测和处理。

(3) How can I find out which programming language specific mode vim is in?

来自 Vim:

set ft?

(4) How can I overwrite that once and for all for a certain class of documents?

要更改当前文件的filetype

:setf <new_filetype>

:setl ft=<new_filetype>

要使更改永久化:从 modeline 执行此操作(参见 :h modeline)。例如:

# vim: filetype=python

你也可以用autocmds达到同样的效果:

autocmd BufRead,BufNewFile *.py setlocal filetype=python

请阅读 :h filetype,其中对所有内容进行了详细描述。