How to Convert Code to HTML With Vim

If you have a code snippet that you want to convert to HTML for posting on the web, you can use Vim, or one of the Vim derivatives such as gVim or Cream.

Just put Vim into normal mode. Generally, that means pressing the ESC key. Then type the following command:

:TOhtml

Vim will output your code as HTML for posting on a web page. If syntax highlighting is on, it will even colorize the code for you. Here is a screenshot of some HTML being HTMLized for posting on the web:

Converting to HTML with Vim

Here is the output of Vim, pasted into this web page:

<html>
<head>
<title>This is a test</title>
</head>
<body>
<h1>Hi there</h1>
<p>This is going to be converted to HTML with Vim.</p>
</body>
</html>

Here is the same code HTMLized with gVim and code coloring:

<html>
<head>
<title>This is a test</title>
</head>
<body>
<h1>Hi there</h1>
<p>This is going to be converted to HTML with Vim.</p>
</body>
</html>

The colorized HTML is not clean, but it works if you need a quick solution for getting some colorized code onto a web page.