Adding YAML syntax highlighting throughout an entire markdown file (in vim) - vim

I currently have the plugin vim-markdown installed. Among other things, it adds syntax highlighting to markdown files. But frequently in markdown files there are yaml headers:
---
yaml: contents
more: yaml
---
# Usual markdown
Etc.
Using vim-markdown, the part of this file enclosed in --- is correctly rendered as yaml (from a syntax highlighting point of a view). The rest of the file is rendered as markdown.
Question: How do I make it so that, no matter where the enclosing --- are located -- yaml is rendered in between?
Attempt: I found in the ~/.vim/bundle/vim-markdown/syntax/markdown.vim the following snippet:
syn include #yamlTop syntax/yaml.vim
syn region Comment matchgroup=mkdDelimiter start="\%^---$" end="^---$" contains=#yamlTop
I removed the \% in the start= field and tried again. But it didn't work :(

In case you have not yet found the solution: Add the following to your .vimrc:
let g:vim_markdown_frontmatter = 1
It is documented here:
https://github.com/plasticboy/vim-markdown

Related

How to add words to vim coloring in C/C++ mode?

I notice that in C/C++ mode, comments such as TODO XXX and FIXME get special color marking.
How can I add the word HACK to this list of words to be marked in the same way?
I tried adding the following to my ~/.vimrc, but it didn't work:
syn keyword cTodo contained TODO FIXME XXX HACK
I would advise against directly modifying the original syntax file; you then have to maintain your version whenever the original changes (e.g. after a Vim upgrade). For these small syntax enhancements, the place is in the ~/.vim/after/syntax/c.vim file, which is sourced after the original syntax. The line would be
syn keyword cTodo contained HACK
You need to modify the syntax file. Typically, it is in /usr/share/vim/vim72/syntax, and the file you want is c.vim and cpp.vim. You will see a line syn keyword cTodo contained followed by a list of words that are considered under the Todo label for coloring. You can add your word there, or make your own keyword, but adding your own keyword would mean adding your keyword to the coloring file as well.
For user only changes, make a directory ~/.vim/syntax. Copy the c.vim and cpp.vim files there, and edit as necessary.
Second edit: Decided to look further, and it appears you can just add to a current syntax file, but I haven't tried it. Add your one line you added to your .vimrc to a file in ~/.vim/after/syntax

How to selectively apply syntax highlight

I'm writing a tutorial/book with Vim, and I'd like to turn off the syntax highlighting for a block of text (the normal text of the book) and reactivate it for the code examples.
I have Googled to no end, but I cannot find a simple solution. Am I missing something? Or is this not achievable in Vim?
I have written the SyntaxRange plugin for that (my main use case is highlighting patches inside emails as with the "diff" syntax). With it, you can :[range]SyntaxIgnore or :[range]SyntaxInclude {filetype} certain sections of a buffer, or, when the sections start and end with certain markers, define dynamic sections that adapt when the number of lines change.
You can create a syntax file for your book.
For example, you can create a script: ~/.vim/syntax/tutor.vim
"
" tutor.vim -- syntax file for my book/tutor
"
syn include #PY $VIMRUNTIME/syntax/python.vim
syn region pyBlock start="{{{" end="}}}" contains=#PY
This is a sample file:
# File: intro.txt
# Date: 2012-08-19
blah, blah ...
So, I will show you some code:
{{{
def hello():
print "world"
}}}
# vim: set syn=tutor :
Thinking tangentially...
How about using something like restructured text or markdown within Vim and render on github. This gives you version management for free. You can have code-blocks.

How to update Vim to color-code new html elements

I am wondering how I might set vim to color the new html5 elements (ie "canvas" and "video") as it does with the existing "script", "body" elements (or reserved words in other languages like python's "def") etc. Current version is from MacPorts typically used in a terminal emulator.
html.vim is the syntax file Vim consults to determine which tags will be colored. The location of this will depend on your installation of Vim. Within this syntax file you'll see many lines that look like the following:
" new html 4.0 tags
syn keyword htmlTagName contained abbr acronym bdo button col label
syn keyword htmlTagName contained colgroup del fieldset iframe ins legend
syn keyword htmlTagName contained object optgroup q s tbody tfoot thead
These lines define syntax keywords. In this case they specifically define HTML tag names. The first line tells Vim to color abbr, acronym, bdo, button, col, and label tags. You can tell Vim to color additional tags with the following syntax:
" new html 5 tags
syn keyword htmlTagName contained video canvas
Vim will now color video and canvas tags and any additional keywords you add.
However if you update the built-in html.vim it will get overwritten the next time you update Vim, so the best practice is to append your rules to these built-in ones. To do so create the folder path after/syntax in your .vim folder and place a html.vim in it.
There are a large number of the HTML 5 elements and arguments in this gist mentioned by #user240515 below. Paste the contents of this into your newly create html.vim.
Consult :help html.vim for some more info.
Thanks for this question, and thanks for the accepted answer! This is a complete list of the new tags to add for html 5, as they are defined at the time of writing:
" new html 5 tags
syn keyword htmlTagName contained article aside audio canvas command datalist
syn keyword htmlTagName contained details embed figcaption figure footer header
syn keyword htmlTagName contained hgroup keygen mark meter nav output progress
syn keyword htmlTagName contained rp rt ruby section source summary time video
I'm just about to try this one:
http://github.com/othree/html5.vim
Seems pretty complete.
EDIT: I don't see anything about indentation. :(
EDIT [12/23/2012]: I do :) But maybe is was added later: https://github.com/othree/html5.vim/tree/master/indent
Just put the following file in ~/.vim/syntax:
http://gist.github.com/390929
Indentation can be supported using an approach similar to that described by michaelmichael for extending the html.vim syntax file. If you don't already have html.vim in ~/.vim/indent you can create it with the content found here. Within the ~/.vim/indent/html.vim you'll see a set of function calls assembling a list of HTML element names that looks like the following:
" [-- <ELEMENT ? - - ...> --]
call <SID>HtmlIndentPush('a')
call <SID>HtmlIndentPush('abbr')
call <SID>HtmlIndentPush('acronym')
call <SID>HtmlIndentPush('address')
" ...and many more...
These lines are defining the tags that will trigger basic tag indenting. Extend this list with any HTML5 tags that you want to have trigger indenting. I added the following to the end of this list:
" New HTML 5 elements
call<SID>HtmlIndentPush('table')
call<SID>HtmlIndentPush('article')
call<SID>HtmlIndentPush('aside')
call<SID>HtmlIndentPush('audio')
call<SID>HtmlIndentPush('canvas')
call<SID>HtmlIndentPush('command')
call<SID>HtmlIndentPush('datalist')
call<SID>HtmlIndentPush('details')
call<SID>HtmlIndentPush('embed')
call<SID>HtmlIndentPush('figcaption')
call<SID>HtmlIndentPush('figure')
call<SID>HtmlIndentPush('footer')
call<SID>HtmlIndentPush('header')
call<SID>HtmlIndentPush('hgroup')
call<SID>HtmlIndentPush('keygen')
call<SID>HtmlIndentPush('mark')
call<SID>HtmlIndentPush('meter')
call<SID>HtmlIndentPush('nav')
call<SID>HtmlIndentPush('output')
call<SID>HtmlIndentPush('progress')
call<SID>HtmlIndentPush('rp')
call<SID>HtmlIndentPush('rt')
call<SID>HtmlIndentPush('ruby')
call<SID>HtmlIndentPush('section')
call<SID>HtmlIndentPush('source')
call<SID>HtmlIndentPush('summary')
call<SID>HtmlIndentPush('time')
call<SID>HtmlIndentPush('video')
Indenting will now be triggered on the HTML5 tags listed above.
I added an html5 indent file to a fork of othree/html5.vim based on the suggestions above.
See http://github.com/briangershon/html5.vim
The syntax/html.vim file that comes with vim (8.0) is very outdated. A good way to keep your syntax highlighting updated is to use a well maintained plugin such as vim-polygot which is kept much more up-to-date. It's html.vim syntax supports, canvas, video, section and main (which other answers do not support), to name a few.

Editing HTML5 with Vim [duplicate]

I am wondering how I might set vim to color the new html5 elements (ie "canvas" and "video") as it does with the existing "script", "body" elements (or reserved words in other languages like python's "def") etc. Current version is from MacPorts typically used in a terminal emulator.
html.vim is the syntax file Vim consults to determine which tags will be colored. The location of this will depend on your installation of Vim. Within this syntax file you'll see many lines that look like the following:
" new html 4.0 tags
syn keyword htmlTagName contained abbr acronym bdo button col label
syn keyword htmlTagName contained colgroup del fieldset iframe ins legend
syn keyword htmlTagName contained object optgroup q s tbody tfoot thead
These lines define syntax keywords. In this case they specifically define HTML tag names. The first line tells Vim to color abbr, acronym, bdo, button, col, and label tags. You can tell Vim to color additional tags with the following syntax:
" new html 5 tags
syn keyword htmlTagName contained video canvas
Vim will now color video and canvas tags and any additional keywords you add.
However if you update the built-in html.vim it will get overwritten the next time you update Vim, so the best practice is to append your rules to these built-in ones. To do so create the folder path after/syntax in your .vim folder and place a html.vim in it.
There are a large number of the HTML 5 elements and arguments in this gist mentioned by #user240515 below. Paste the contents of this into your newly create html.vim.
Consult :help html.vim for some more info.
Thanks for this question, and thanks for the accepted answer! This is a complete list of the new tags to add for html 5, as they are defined at the time of writing:
" new html 5 tags
syn keyword htmlTagName contained article aside audio canvas command datalist
syn keyword htmlTagName contained details embed figcaption figure footer header
syn keyword htmlTagName contained hgroup keygen mark meter nav output progress
syn keyword htmlTagName contained rp rt ruby section source summary time video
I'm just about to try this one:
http://github.com/othree/html5.vim
Seems pretty complete.
EDIT: I don't see anything about indentation. :(
EDIT [12/23/2012]: I do :) But maybe is was added later: https://github.com/othree/html5.vim/tree/master/indent
Just put the following file in ~/.vim/syntax:
http://gist.github.com/390929
Indentation can be supported using an approach similar to that described by michaelmichael for extending the html.vim syntax file. If you don't already have html.vim in ~/.vim/indent you can create it with the content found here. Within the ~/.vim/indent/html.vim you'll see a set of function calls assembling a list of HTML element names that looks like the following:
" [-- <ELEMENT ? - - ...> --]
call <SID>HtmlIndentPush('a')
call <SID>HtmlIndentPush('abbr')
call <SID>HtmlIndentPush('acronym')
call <SID>HtmlIndentPush('address')
" ...and many more...
These lines are defining the tags that will trigger basic tag indenting. Extend this list with any HTML5 tags that you want to have trigger indenting. I added the following to the end of this list:
" New HTML 5 elements
call<SID>HtmlIndentPush('table')
call<SID>HtmlIndentPush('article')
call<SID>HtmlIndentPush('aside')
call<SID>HtmlIndentPush('audio')
call<SID>HtmlIndentPush('canvas')
call<SID>HtmlIndentPush('command')
call<SID>HtmlIndentPush('datalist')
call<SID>HtmlIndentPush('details')
call<SID>HtmlIndentPush('embed')
call<SID>HtmlIndentPush('figcaption')
call<SID>HtmlIndentPush('figure')
call<SID>HtmlIndentPush('footer')
call<SID>HtmlIndentPush('header')
call<SID>HtmlIndentPush('hgroup')
call<SID>HtmlIndentPush('keygen')
call<SID>HtmlIndentPush('mark')
call<SID>HtmlIndentPush('meter')
call<SID>HtmlIndentPush('nav')
call<SID>HtmlIndentPush('output')
call<SID>HtmlIndentPush('progress')
call<SID>HtmlIndentPush('rp')
call<SID>HtmlIndentPush('rt')
call<SID>HtmlIndentPush('ruby')
call<SID>HtmlIndentPush('section')
call<SID>HtmlIndentPush('source')
call<SID>HtmlIndentPush('summary')
call<SID>HtmlIndentPush('time')
call<SID>HtmlIndentPush('video')
Indenting will now be triggered on the HTML5 tags listed above.
I added an html5 indent file to a fork of othree/html5.vim based on the suggestions above.
See http://github.com/briangershon/html5.vim
The syntax/html.vim file that comes with vim (8.0) is very outdated. A good way to keep your syntax highlighting updated is to use a well maintained plugin such as vim-polygot which is kept much more up-to-date. It's html.vim syntax supports, canvas, video, section and main (which other answers do not support), to name a few.

How to syntax highlight fragments of code in one language embedded in the source code in another language in Vim?

I have a custom XML file format which can contain blocks of code within certain tags.
For example:
<Root>
<Sql> select * from foo </Sql>
<MoreJunk> ... </MoreJunk>
<Python><![CDATA[
def Bar(*args):
return False
]]></Python>
</Root>
How can I get Vim to use SQL syntax highlighting for the text inside <Sql> tags and use Python higlighting for text inside <Python> tags?
I know Vim can already do this because it correctly highlights Javascript inside HTML files.
I tried inspecting the HTML syntax file but couldn’t figure it out.
For your XML with python example you would have to do something like this:
runtime! syntax/xml.vim
unlet b:current_syntax
syntax include #Python syntax/python.vim
syntax region pythonCode start=+<Python>+ keepend end=+/</Python>+ contains=#Python
These lines will include the XML syntax and the python syntax, and the specify a python region where VIM will use the python syntax instead of the XML syntax...
Of course, all this is well documented in VIM. See :he :syn-include on how to include syntax files.
This document describes how to write your own syntax highlighting. You should probably be able to figure out how the HTML-syntax highlighting works with javascript, with that as a reference.

Resources