Search code examples
vimsyntax-highlightingvim-syntax-highlighting

how can i assign indented block in vim a special syntax highlighter?


for convenience in grouping couchdb functions i created a file format that groups separate things together using yaml

it basically contains entries in the form of name.ext: | followed by a intended block of code in the language fitting to .ext

for more pleasant editing i'd like to have vim use the correct syntax highlighters for them

edit

some code examples as requested

simple:

map.coffee: |
  (doc) ->
    for item in doc.items:
     emit [doc.category, item], null
    return

reduce: _count

more complex:

map.coffee: |
  (doc) ->
    emit doc.category, {items: 1, val: doc.value}
    return

reduce.coffee: |
  (keys, values, rereduce) ->
    ret = {items: 0, val: 0}
    for v in values
      ret.items += doc.items
      ret.val += doc.val
    return ret

Solution

  • I believe that what you want it to make use of Vim's syntax regions (:help syn-region). But regions take delimiters as parameters.

    You have a well defined start but not a defined end, maybe you could work your way around by establishing some conventions here like "2 empty new lines at the end".

    There are similar answers that might give you a hint (including the docs) on how to implement a solution, like: Embedded syntax highligting in Vim

    Also interesting and similar approach is this Vimtip: http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file