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

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

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

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

它是如何做到这一点的?

编辑:让我补充一下,我正在运行 Ubuntu Server 12.04 LTS (Precise Pangolin)

答案1

vim(现在在大多数系统上vi实际上是 的符号链接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.

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

相关内容