This question already has an answer here:

I would like that for files with .vm extension vim would:

  • colour in red all the words between two back-ticks like `this`,
  • colour in yellow what is between two dollars like $this is in yellow$,
  • and colour in green all words between # and the end of the line.

marked as duplicate by Heptite, DavidPostill, techraf, mdpc, Tetsujin Oct 10 '16 at 6:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Syntax script

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

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

syntax match vmBackticks "`\zs[^`]\+\ze`"
syntax match vmDollars "\$\zs[^$]\+\ze\$"
syntax match vmComment "#.*$"

hi def link vmBackticks Identifier
hi def link vmDollars Statement
hi def link vmComment Comment

let b:current_syntax = 'vm'

If you want to include the backticks / dollars in the highlighting, just drop the \zs and \ze.

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=vm to active. To do this automatically, you have to teach Vim about your new filetype.

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

autocmd BufNewFile,BufRead *.vm setf vm

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.

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