{"id":496,"date":"2015-11-18T20:49:06","date_gmt":"2015-11-18T20:49:06","guid":{"rendered":"http:\/\/blog.knofafo.de\/?p=496"},"modified":"2015-11-18T20:49:06","modified_gmt":"2015-11-18T20:49:06","slug":"creating-your-own-syntax-highlighting-rules-for-vim","status":"publish","type":"post","link":"https:\/\/blog.knofafo.de\/?p=496","title":{"rendered":"Creating your own syntax highlighting rules for VIM"},"content":{"rendered":"<p>This ought to be some kind of tutorial on how to write your own syntax highlighting rules for vim and how to activate them. Actually, the sites I found on the web, were either too superficial or much too complicated &#8211; like the vim documentation itself. I used them all to learn the art of syntax highlighting. Still, I am no master. Anyhow, there is a bit of pride about my success.<\/p>\n<p><!--more--><\/p>\n<h5>What is syntax highlighting?<\/h5>\n<p>Assume you have some random text &#8211; pure text without any formatting. Now, you want to have certain parts, phrases, words highlighted &#8211; specific background, font color, font decoration. Stick to a limited number of classes. Example: Every word beginning with &#8220;anti&#8221;. Label your classes. All the text segments that shall be highlighted have to be assigned a label which then defines how your text is highlighted.<\/p>\n<p>Syntax highlighting makes your text easily readable. On a very low level syntax highlighting can even help to avoid errors. Originally, I got to know syntax highlighting from coding. You have different colors for variables, function names, comments and so on. In preparing this tutorial I aimed for something very different. In my new job I work a lot with text &#8211; as an editor I have to refine text handed in by authors in a way that it fits our standards. I&#8217;m working for a german journal. For instance, passive voice is considered bad style. Also words like &#8220;for instance&#8221; (zum Beispiel) or &#8220;also&#8221; (auch) are not well liked. Syntax highlighting shall help to make such mistakes visible.<\/p>\n<h5>Defining syntax rules<\/h5>\n<p>The simplest case is the appearance of a single word. The following example, which is valid for vim syntax files, assigns the word <code>Foo<\/code> to the class <code>Foo<\/code>:<\/p>\n<p><code>syntax keyword Foo Foo<\/code><\/p>\n<p>You might wonder where this piece of code shall be written to. It is a vim command so you can simply enter in vim but a more appropriate place is a new file in the syntax folder under your vim configuration path. My Kubuntu and my Debian installation place it under <code>~\/.vim\/syntax<\/code>. For the purpose of this tutorial we create highlighting for the Filetype <code>bar<\/code>. Hence, the syntax file is <code>~\/.vim\/syntax\/bar.vim<\/code>.<\/p>\n<p>The next step is to define how the class Foo is highlighted. This can be achieved by the following line:<\/p>\n<p><code>highlight Foo cterm=bold,underline<\/code><\/p>\n<p>In a more understandable language this means: &#8220;Dear computer, please highlight every piece of my text which is classified as Foo by using bold font and underlining it, as long as we&#8217;re on a color terminal!&#8221; By adding &#8220;term=bold,underline&#8221; a similar rule is defined for a monochrome terminal and, eventually, we can define rules for &#8220;gui&#8221; in a similar way.<\/p>\n<p>The syntax so far should look like:<\/p>\n<p><code><br \/>\nif exists(\"b:current_syntax\")<br \/>\nfinish<br \/>\nendif<\/code><\/p>\n<p>syntax keyword Foo Foo<br \/>\nhighlight Foo cterm=bold,underline<\/p>\n<p>let b:current_syntax=bar<\/p>\n<p>There is some yet unexplained decoration. They prevent several multiple syntax files to be loaded at the same time and are not required &#8211; but nice to have.<\/p>\n<h5>Activating the style file<\/h5>\n<p>Since you most likely created the style file within VIM you can now directly switch to your new highlighting by using the command: <code>set syntax=bar<\/code>. The result should look something like:<\/p>\n<div id=\"attachment_502\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_01.png\"><img aria-describedby=\"caption-attachment-502\" loading=\"lazy\" class=\"wp-image-502 size-medium\" src=\"http:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_01-300x123.png\" alt=\"All Foo are set in bold font and underlined\" width=\"300\" height=\"123\" srcset=\"https:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_01-300x123.png 300w, https:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_01.png 720w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-502\" class=\"wp-caption-text\">All &#8220;Foo&#8221;s are set in bold font and underlined<\/p><\/div>\n<p>Instead of defining every highlighting class on your own one cal also revert to predefined classes like: Todo, Comment, Statement, Type, Constant, PreProc. Instead of the above our syntax file could contain the line<\/p>\n<p><code>hi link Foo Error<\/code><\/p>\n<p>In that case the example looks different:<\/p>\n<div id=\"attachment_504\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_02.png\"><img aria-describedby=\"caption-attachment-504\" loading=\"lazy\" class=\"size-medium wp-image-504\" src=\"http:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_02-300x145.png\" alt=\"All Foo have a red background\" width=\"300\" height=\"145\" srcset=\"https:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_02-300x145.png 300w, https:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/10\/example_02.png 642w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-504\" class=\"wp-caption-text\">All Foo have a red background<\/p><\/div>\n<h5>More about the <code>highlight<\/code> command<\/h5>\n<p>We saw how to link a new highlighting class against an existing one and now let me tell you a bit more about defining your own style &#8211; this tutorial won&#8217;t go any deeper. We have three relevant terminals: gui &#8211; which you encounter for instance when using <i>gvim<\/i>, cterm &#8211; probably used most often on modern computers; it belongs to the normal shell version of vim on a terminal with colored output, and term &#8211; the monochrome variant of cterm; of course, no color options are possible with this term. A situation where the latter becomes relevant is printing on a grayscale printer.<\/p>\n<p>For all terminals you can define (note the the abilities of the frontends differ):<\/p>\n<ul>\n<li>font appearance: <code>term|cterm|gui={list}<\/code> &#8212; any (comma-separated) combination from: bold, underline, undercurl, reverse, inverse, italic, standout, NONE, where NONE resets previosly defined settings<\/li>\n<li>font color: <code>ctermfg|guifg={color}<\/code> &#8212; in cterm the color is an integer or a color name, for the gui you can additionally use HTML color codes<\/li>\n<li>background color: <code>ctermbg|guibg={color}<\/code> &#8212; same as above<\/li>\n<\/ul>\n<p>Example:<br \/>\n<code><br \/>\nhi Foo ctermbg=Green ctermfg=White cterm=bold,underline<br \/>\n<\/code><\/p>\n<p>Sidenote: vim-commands may be abbreviated. Just strip letters from the end of the command. This is fine as long as the remainder is still unique. So just type <code>hi<\/code> instead of <code>highlight<\/code> or <code>syn<\/code> instead of <code>syntax<\/code>.<\/p>\n<h5>Defining syntax elements<\/h5>\n<p>We started with a simple keyword. But that&#8217;s not the power of syntax hightlight &#8212; although it is important. Since this tutorial is not aiming at explaing how to use regexps within vim-files or even in general, I encourage to make sure you know enough.<\/p>\n<p>The rules are simple enough:<br \/>\n<code><br \/>\nsyn region textregion start=\"pattern1\" end=\"pattern2\"<br \/>\nsyn match textpattern \"pattern\"<\/p>\n<p>hi textregion ctermbg=white ctermfg=green<br \/>\nhi textpattern ctermbg=green ctermfg=white<br \/>\n<\/code><\/p>\n<p>One picture cann tell more than a hundred words, so just have look:<br \/>\n<a href=\"http:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/11\/example_pattern.png\"><img loading=\"lazy\" src=\"http:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/11\/example_pattern-300x69.png\" alt=\"example_pattern\" width=\"300\" height=\"69\" class=\"aligncenter size-medium wp-image-527\" srcset=\"https:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/11\/example_pattern-300x69.png 300w, https:\/\/blog.knofafo.de\/wp-content\/uploads\/2015\/11\/example_pattern.png 462w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>More about patterns can be found here: <a href=\"http:\/\/vimdoc.sourceforge.net\/htmldoc\/pattern.html#pattern\" target=\"_blank\">Vim documentation # patterns<\/a><\/p>\n<h5>Finalization<\/h5>\n<p>I assume you are now able to build at least a small, complete syntax file. On Linux you place it unter <code>~\/.vim\/syntax<\/code>. Now you want vim to automatically load your syntax definitions when the corresponding file is loaded. To this end make sure that syntax highlight is enabled. Your .vimrc (<code>~\/.vimrc<\/code> in Linux) should contain a line <code>syntax on<\/code>. <\/p>\n<p>For automatic loading I used the filetype command. In <code>~\/.vimrc<\/code> it activates filetype checking through the command <code>filetype on<\/code>. The actual attributing is done in the the file <code>~\/.vim\/ftdetect\/bar.cim<\/code>. It contains one line which enables our previously defined syntax highlighting for all files ending with .bar<\/p>\n<p><code><br \/>\nau BufRead,BufNewFile *.bar set filetype=bar<br \/>\n<\/code><\/p>\n<p>This command sets filetype to bar for every new or opened file with a .bar suffix.<\/p>\n<p>Note that you can have more complicated criteria which are sensitive for file content. I haven&#8217;t gone through these rules. With the things presented here you are able to write your own syntax files. Later refinements are easy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This ought to be some kind of tutorial on how to write your own syntax highlighting rules for vim and how to activate them. Actually, the sites I found on the web, were either too superficial or much too complicated &#8211; like the vim documentation itself. I used them all to learn the art of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[3,4,31],"tags":[38,42],"_links":{"self":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts\/496"}],"collection":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=496"}],"version-history":[{"count":17,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts\/496\/revisions"}],"predecessor-version":[{"id":534,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts\/496\/revisions\/534"}],"wp:attachment":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}