Vim

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

  • May 1, 2014

當我在 中編輯配置文件時vi,似乎vi知道文件的語法。

例如,vi將根據其是鍵還是值來以一種或另一種方式著色標記。此外,vi似乎也知道哪些值是有效鍵。

它是如何做到的?

編輯:讓我補充一下,我正在執行 Ubuntu Server 12.04 LTS (Precise Pangolin)

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.

引用自:https://unix.stackexchange.com/questions/127411