When I edit configuration files in vi, it seems like vi is aware of the syntax of the file.

For example, vi will color tokens one way or another depending on whether its a key or a value. Furthermore, vi also seems to be aware of which values are valid keys.

How does it do this?

Edit: Let me add that I'm running Ubuntu Server 12.04 LTS (Precise Pangolin)

1 Answer 1


vim (on most systems these days vi is actually a symlink for vim) uses syntax files to define the coloring schemes for the various languages it can deal with. You have not specified which OS you use but on my LMDE system, these are found in /usr/share/vim/vim74/syntax/.

When you open a file using vim, it will first try and figure out what type of file it is. As explained in the official documentation:

Upon loading a file, Vim finds the relevant syntax file as follows:

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

So, basically, vim uses some tricks to parse and guess the file type and then will load the appropriate syntax file. The file that defines the syntax for configuration files is /usr/share/vim/vim74/syntax/config.vim.

  • That explains it! I was originally editing /etc/dnsmasq.conf and I was baffled as to how vi seemed to know how to color all the parameters. Lo and behold, I was able to find /usr/share/vim/vim73/syntax/dnsmasq.vim.
    – AlfaZulu
    May 1, 2014 at 12:54
  • @AlfaZulu take into account that this is essentially true for all debian based distros.
    – Braiam
    May 1, 2014 at 13:02
  • 1
    You can use :set syn to find out the name of the current syntax (and to change it, if you want to use another one).
    – deltab
    May 1, 2014 at 14:49
  • 3
    A minor technicality: there are two processes involved. The syntax files are responsible for categorizing pieces of text (this string is an Integer), and a color scheme is used to define how different categories are displayed (make Integers blue).
    – chepner
    May 1, 2014 at 18:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .