Open Source Software Technical Articles

Want the Best of the Wazi Blogs Delivered Directly to your Inbox?

Subscribe to Wazi by Email

Your email:

Connect with Us!

Current Articles | RSS Feed RSS Feed

Create Your Own Syntax Highlighting in Vim


In text editors like Vim, syntax highlighting is the feature that shows defined components in different colors depending on their functions in the file. Vim has code syntax highlighting already available for a huge array of programming languages, including PHP, Python, HTML, Ruby, JavaScript, and more, and most of them are installed by default in the $VIM/syntax/ directory, so in most cases you can just open up your source code files and see different parts appropriately color-coded. But what if you're using a little-known language that doesn't have a preinstalled syntax file? Or if you're working with a different type of file altogether and want to create syntax highlighting from scratch? This tutorial shows how easy and even fun it is to write your own syntax highlighting rules for Vim.

Before you start building your own syntax highlighting rules, it may help to look at an existing set of rules. There are two ways you can do this:

    1. Open a file for which Vim has preloaded rules (e.g. an HTML file) and type :syntax.

    1. Edit one of the *.vim files in $VIM/syntax/.

Most language syntax files have a lotof rules, so don't get overwhelmed; just take a quick look at the style of the rules.


An easy way to get the right setup for your new syntax file is to copy one of the existing files from $VIM/syntax/ and then delete most of its content, keeping only what you need and modifying it as you like. Alternatively, just create a file with the entries here. The example I'm going to use is for a todo list, with categories such as work, home, and personal. It might look a bit like this:

h: sort out drawers in spare room
w: draft Vim highlighting article
p: email

The syntax highlighting will give the different categories different colors.


Create a todo.vim file and start it off like this:

" Vim syntax file
" Language: Juliet's todo files
" Maintainer: Juliet Kemp
" Last Change: Sept 14, 2011
" Version: 1

if exists("b:current_syntax")

setlocal iskeyword+=:
syn case ignore

This header is standard for preinstalled syntax files, so it's good practice to use it (or something similar) for your files too. " is the comment character for Vim syntax files. The ifclause checks for any already defined syntax highlighting, and abandons processing here if it finds one. As a rule, this shouldn't happen, but it would be possible to have a global setting to read one file and a local setting to read another; it's best practice to have a clause like this to avoid any possible confusion.


The next line tells Vim that the keywords we'll use include the : character. Keywords match the iskeyword option (use the command :set isk ? to find out what it is currently set to), which should by default include digits, alphabetic characters, /, @, and _. If you have any other punctuation characters in your keywords, you'll need to set them up, as we do here.

The following line tells Vim to ignore case when matching keywords. You can use case match instead if that's more suitable for your language or file type.

Next, add a couple of actual rules:

syn match todoHome "^h: "
syn match todoWork "^w: "
syn match todoPersonal "^p: "

syn match looks for a specific regular expressionto match. Here, we're looking for lines starting with a particular letter followed by a colon [and a space], and attaching a syntax label to each match.


Next, we have to set the highlights that apply to each label:

highlight link todoHome Type
highlight link todoWork String
highlight link todoPersonal Statement

Type, String, Statement, and others are preset groups that are associated with defined formats. You can see the full list on the Vimdoc website. Unfortunately the site doesn't list colors used for each, so you'll have to experiment to get something you like. If you're setting up a new syntax file for a language, you should probably stick with the group names that are closest to the language elements that you're highlighting, for maximum compatibility with what you and others are already used to.


Finally, set the name of the current syntax file:

let b:current_syntax = "todo"

Now you can save the file.



Loading a Syntax File

The next task is to make sure your syntax definitions get loaded the next time you open a todo list. The usual way Vim identifies files is by extension. We'll call any todo list files *.todo, and add this line to ~/.vimrc:

autocmd BufRead,BufNewFile *.todo set filetype=todo

The filetype references the name of the syntax file: here, todo.vim. You also need to make sure that ~/.vimrc includes the line:

syntax on

This turns syntax highlighting on in Vim. It will often be set by default, but it's a good idea to set it explicitly. Save the changes, and open a sample test.todo file and enter some appropriate lines. You should see the syntax highlighting working. If you want to change anything (for example, add a rule, or change the setting for the highlighting colors), edit todo.vim and save it, then type :syntax onin your test.todo buffer, and the new syntax rules will automatically be loaded.


Matching Regions

Currently, only the initial labels are highlighted. To highlight a whole line, you need to use a region match instead. Try replacing your syn match lines with these:

syn region todoHome start=/^h: / end=/
syn region todoWork start=/^w: / end=/
syn region todoPersonal start=/^p: / end=/

The syntax is very similar to syn match. It defines a label, then a start and an end pattern of the region. As these rules stand, they look for a newline (
; alternatively, you can use $to match end-of-line), so they give you only one-line highlighting. You can play around to adapt this if you want multiple-line todo items.


You can set up syntax highlighting such that certain matches get "priority." For example, these lines look for email addresses (as defined by the regular expression in quotes – and this pattern is imperfect, so don't rely on it for anything vital) and set their color:

syn match todoEmail "[a-zA-Z0-9._-]\+@[a-zA-Z0-9./-]\+"
highlight link todoEmail Identifier

However, as things stand, the todoHome and other defined regions override this, so you won't get the email address highlighting you expect until you alter the syn region lines:

syn region todoHome	start=/^h: / end=/
/ contains=todoEmail

The contains element tells Vim to look for this type of element contained in the region area, and then to highlight it appropriately. You can have as many labels as you like in this section. If the definition of the contained element also includes an end-of-line character (here, it doesn't), add keepend at the end of the syntax definition to avoid the end-of-line being "swallowed" and the containing element running over into the next line:

syn region todoHome	start=/^h: / end=/
/ contains=todoEmail keepend


To take this a bit further, you can also use the contained keyword to limit matches to occur only within certain sections. For example, if you wanted to highlight email addresses only within a labeled line, and not if you'd just made a note at the bottom of the file, you could use this syntax:

syn match todoEmail contained "[a-zA-Z0-9._-]\+@[a-zA-Z0-9./-]\+"

This means that the match is valid only if it is contained within another syntax element that is specified as contains todoEmail. So if you had these lines as well:

syn region todoHome start=/^h: / end=/
syn region todoWork start=/^h: / end=/
/ contains=todoEmail

then an email address would be highlighted only if it was contained in a todoWork region.


You might want to highlight todo items you've completed. You can add a "done" setting for when you add "x" at the start of lines:

syn region todoDone start=/^x[hwp]: / end=/
highlight link todoDone Underlined

If you want "done" lines to be invisible (but not deleted), you could use Ignore instead of Underlined.


Going Further

All of the above is fine if what you're defining is single, specific matches. But if you're writing syntax for a whole language, you might want an easier way to do it. This is where syn keyword comes in. Suppose you were writing a syntax file for Perl, and you want to group various sorts of blocks together:

syn keyword groupBlockLabels if while for

You could then define the highlight color with highlight linkas above.


And, of course, it's possible to get a lot more complicated. For instance, you can use the nextgroup option to specify that a certain sort of match should always appear after a certain keyword. After the groupBlockLabels element above, you might want to define a code block, beginning and ending with curly braces, and then specify that this type of block should always occur after a groupBlockLabels element:

syn region codeBlock start=/{/ end=/}/
syn keyword groupBlockLabels if while for nextgroup=codeBlock

In the todo example, you could use a character other than a newline to end the text part of the todo, put a completion date after that, and use nextgroupto specify that a date should come after a todo list item.


One of the best ways to get to grips with syntax highlighting in Vim is to experiment. For more information to support your experimentation, check out the Vim documentation chapter on writing syntax files. In particular, check out the matchgroup and transparent options for more complicated setups. Happy highlighting – and feel free to share your best syntax highlighting tips and tricks in the comments section below!

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.


A simple approach to get the right setup for your new punctuation document is to duplicate one of the existing records from $vim/syntax/ and afterward erase a large portion of its substance, keeping just what you need and altering it as you like. On the other hand, simply make a record with the sections conversion service 
Posted @ Saturday, March 22, 2014 3:54 AM by data conversion service
This type of planning for choosing a good application design company will believe every people because these are very good as well as nice and precious.
Posted @ Saturday, March 29, 2014 8:04 AM by
Thats wounderful advice from you, but I want some quick results treatments. I hope you will soon post some more ideas.
Posted @ Monday, March 31, 2014 5:33 AM by tracypaul2
Thank you for sharing this article. Fake Ray Bans RB2140 It is really useful for me.
Posted @ Wednesday, May 28, 2014 3:37 AM by ronmance
The wholesale prices of these models range from only. Whereas when decides to throw them open in to the retail market they could usually sell it in a range of per watch.It is a clear cut profit of around 90% on the sale of each watch.Is it not amazing? Of all the watches the Wholesale Replica Rolex watches has a quick movement as they come in lots of models for men,ladies boys as well.The wholesale replica Movado watches are also worthy of mention. Much sought after brand of watches they spell sheer elegance style no wonder become an instant hit with the wholesalers the retailers asking for more of the same brand.Invariably all the watches are accompanied with authentic sapphire crystals,high grade materials, true Swiss movements. Placing order on this site is a simple process the user friendly web-site usually supports in carrying forward the necessary procedures.Usually when the orders are in bulk the wholesale prices also could vary.Genuine Swiss craftsmanship top quality materials used in the manufacture of these watches makes the acquisition of LV monogram vernis wholesale Swiss made Rolex replica watches a great assured of great profits satisfaction for the acquisition they have made because of the quality extravagance they come with.
Posted @ Thursday, June 26, 2014 8:11 PM by vuitton
There are so many comments here that are really interesting and useful to me thanks
Posted @ Friday, July 11, 2014 2:40 AM by motorcycle club names
Posted @ Friday, July 18, 2014 6:43 AM by Addiction Treatment
Coach Outlet Online Still, California has begun construction of the tracks and put out bids for Coach Factory Outlet a vendor to build the trains. And the new rail project will get an infusionCoach Outlet of funds from the state’s cap-and-trade program, which requires business to pay forCoach Outlet Online excess pollution.Florida, Ohio and Wisconsin, all led by Republican governors,Coach Outlet Store canceled high-speed rail projects and returned federal fundsCoach Outlet Store Online after deeming the projects too expensive and unnecessary.Coach Outlet The Obama administration’s management of previously appropriated high-speed railCoach Factory Online funding has been as clumsy as its superintending of the Affordable Care ActCoach Factory Outlet s rollout,” said Frank N. Wilner, a former chief of staff at theCoach Factory Outlet Surface Transportation Board, a bipartisan body with oversight of the nation’s railroads.Coach Outlet When Mr. Obama first presented his vision for high-speed rail nearly four years agoCoach Outlet Store Online he described a future of sleek bullet trains hurtling passengersChanel Outlet Online between far-flung American cities at more than 200 m.p.h.Louis Vuitton Outlet Within 25 years, our goal is to give 80 percentLouis Vuitton Handbags of Americans access to high-speed rail,” Mr. Obama saidGucci Belt in his 2011 State of the Union address. “This could allow you to go places in halfGucci Belts the time it takes to travel by car. For some trips, it will be faster than flying — without the pat-down.
Posted @ Saturday, August 09, 2014 8:36 AM by guoguo
Post Comment
Website (optional)

Allowed tags: <a> link, <b> bold, <i> italics