Vim – Creating Simple Syntax Highlighting

syntax highlightingvim

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

Best Answer

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
    
Related Question