36

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

variable=value

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

2 Answers 2

52

Assuming your file's extension is *.foo

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

    $HOME/.vim/ftdetect/foo.vim
    $HOME/.vim/syntax/foo.vim
    
  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
    
3
  • 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. Nov 23, 2014 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
    – user373230
    Nov 24, 2014 at 2:43
  • 1
    You need syntax on in your vimrc.
    – romainl
    Nov 24, 2014 at 7:04
35

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.

You must log in to answer this question.