`vi` 如何知道配置文件的格式?

Alf*_*ulu 5 vim vi syntax-highlighting

当我在 中编辑配置文件时vi,似乎vi知道文件的语法。

例如,vi将根据它是键还是值来以一种或另一种方式为标记着色。此外,vi似乎还知道哪些值是有效键。

它是如何做到这一点的?

编辑:让我补充一点,我正在运行 Ubuntu Server 12.04 LTS(精确穿山甲)

ter*_*don 10

vimvi如今在大多数系统上实际上是 的符号链接vim)使用语法文件来定义它可以处理的各种语言的配色方案。您尚未指定您使用的操作系统,但在我的 LMDE 系统上,这些可以在/usr/share/vim/vim74/syntax/.

当您使用 打开文件时vim,它会首先尝试找出文件的类型。如官方文档中所述

加载文件时,Vim 找到相关的语法文件如下:

Loading the file triggers the BufReadPost autocommands.
|
+-  If there is a match with one of the autocommands from |synload-3|
|   (known file types) or |synload-4| (user's file types), the 'filetype'
|   option is set to the file type.
|
+-  The autocommand at |synload-5| is triggered.  If the file type was not
|   found yet, then scripts.vim is searched for in 'runtimepath'.  This
|   should always load $VIMRUNTIME/scripts.vim, which does the following.
|   |

|   +-  Source the user's optional file, from the *myscriptsfile*
|   |   variable.  This is for backwards compatibility with Vim 5.x only.
|   |
|   +-  If the file type is still unknown, check the contents of the file,
|       again with checks like "getline(1) =~ pattern" as to whether the
|       file type can be recognized, and set 'filetype'.
|
+-  When the file type was determined and 'filetype' was set, this
|   triggers the FileType autocommand |synload-6| above.  It sets
|   'syntax' to the determined file type.
|
+-  When the 'syntax' option was set above, this triggers an autocommand
|   from |synload-1| (and |synload-2|).  This find the main syntax file in
|   'runtimepath', with this command:
|       runtime! syntax/<name>.vim
|
+-  Any other user installed FileType or Syntax autocommands are
triggered.  This can be used to change the highlighting for a specific
syntax.
Run Code Online (Sandbox Code Playgroud)

所以,基本上,vim使用一些技巧来解析和猜测文件类型,然后将加载适当的语法文件。定义配置文件语法的文件是/usr/share/vim/vim74/syntax/config.vim.

  • 一个次要的技术性:涉及两个过程。语法文件负责对文本片段进行分类(此字符串是一个整数),并且使用配色方案来定义不同类别的显示方式(使整数变为蓝色)。 (3认同)