i have a simple sort of a database file which consists only of entries in the following format


i want to create a simple vim syntax highlighting for it and set it for specific file extension

for instance, variable part could be light blue, and value part light red

i googled it and came across things such as $vimruntime\syntax\, syntax set=, syntax match, and hi keywords, but couldn't set it up myself eventually

so i want a very simple vim code snippet that would realize it by matching the left and right hand sides and coloring them separately


Assuming your file's extension is *.foo

  1. Create these files and directories if they don't exist:

  2. Put the following in $HOME/.vim/ftdetect/foo.vim:

    autocmd BufRead,BufNewFile *.foo set filetype=foo
  3. Put the following in $HOME/.vim/syntax/foo.vim:

    syntax match FooKey   /^[^=]\+/
    syntax match FooValue /[^=]\+$/
  4. Put the following lines at the very end of $HOME/.vimrc (or at least after any colorscheme line):

    highlight FooKey   ctermfg=cyan guifg=#00ffff
    highlight FooValue ctermfg=red  guifg=#ff0000
  5. Make sure you have the following line somewhere in your ~/.vimrc:

    syntax on
  • Wow, the OP got two almost identical solutions for the price of one! I like how both of our solutions are very similar, yet distinct in details. – Ingo Karkat Nov 23 '14 at 20:23
  • it works, but again i have to manually type ftype= everytime in vim, there was no fdetect directory in $vimrnutime (i'm on windows, %programfiles(x86)%\vim\vim74\ ), i created it, but still it doesn't react – w17t Nov 24 '14 at 2:43
  • You need syntax on in your vimrc. – romainl Nov 24 '14 at 7:04

Syntax script

Create a file ~/.vim/syntax/simple.vim with the following contents:

" Quit when a syntax file was already loaded.
if exists('b:current_syntax') | finish|  endif

syntax match simpleVar "\k\+" nextgroup=simpleAssignment
syntax match simpleAssignment "=" contained nextgroup=simpleValue
syntax match simpleValue ".*" contained

hi def link simpleVar Identifier
hi def link simpleAssignment Statement
hi def link simpleValue String

let b:current_syntax = 'simple'

That matches the three syntax elements, and provides default colors. One doesn't generally define explicit colors, but instead links to default highlighting groups defined by your colorscheme. You can list all via :hi. For tweaking, read :help :syntax and :help usr_44.txt, and have a look at the syntax scripts that ship with Vim.

Filetype detection

So far, you have to manually :set syntax=simple to active. To do this automatically, you have to teach Vim about your new filetype.

Create a file ~/.vim/ftdetect/simple.vim with the following contents:

autocmd BufNewFile,BufRead *.simple setf simple

This assumes that the files can be identified via the file name (cp. :help autocmd-patterns). You can also detect based on the path (file location), or even the contents. :help new-filetype has details.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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