Syntax Highlighter – Found!

It turns out it was under my nose the whole time!

I was looking at the VIM syntax-highlighting files to see if it’s built-in syntax highlighter was in any shape to be ripped off when I came across a file called “2html.vim”.

Apparently its a VIM script to allow you to convert whatever file you’ve currently open in VIM into HTML, syntax highlighting included!

As it turns out, the “2html.vim” script can be activated by simply selecting “Convert to HTML” from the “Syntax” menu in GVIM (The graphical user interface version of VIM), since I’m a long-time VI user I’m used to running all VIM commands form the keyboard and never gave the GUI menu-bar a second look.

So far it seems that by using that script, GVIM can be the answer to all my requirements of a syntax highlighter, apart from the requirement for the tool to be primarily a command line tool, however, that can be fixed with some creative scripting.

The “2html.vim” script seems to be very flexible, you can customize the HTML it generates by defining some variables within VIM prior to running it, you can set and unset variables by running the “let” and “unl” commands, for example:

:let  html_number_lines = 1

Would set the “html_number_lines” varibale while

:unl  html_number_lines

Would unset that variable.

Here are some of the more useful variables you can use with “2html.vim”:

  • html_number_lines – Causes VIM to generate line-numbers within the HTML, line numbers will also be generated is you’ve already toggled then on within VIM (with “set ln”).
  • html_use_css – By default “2html.vim” utilizes <font> tags to do the actual syntax-highlighting, by setting this variabl, you can make it utilize CSS and <span> tags generate strictly HTML4 compliant code (including the proper DOCTYPE tag!).
  • use_xhtml – Make the script generate XHTML rather then HTML.
  • html_no_pre – Setting this variable causes the script to avoid wrapping the generated text in <pre> tags.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s