Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

share|improve this question
up vote 9 down vote accepted

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.

share|improve this answer
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 '14 at 12:54
@AlfaZulu take into account that this is essentially true for all debian based distros. – Braiam May 1 '14 at 13:02
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 '14 at 14:49
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 '14 at 18:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.