2009/10/05 by Girish Venkatachalam
Girish Venkatachalam is a UNIX hacker with more than a decade of
networking and crypto programming experience.
His hobbies include yoga,cycling, cooking and he runs his own
business. Details here:
People who have a necessity to publish source code for public view like
myself and Tony will benefit by this article.
It is rather funny since I have published a lot of code in this very
site without syntax highlighting. Perhaps things will change with this
Let me quickly cut to the chase. This article comes close on the heels
of my Vim article
and you shall see in a moment why.
Both my tips involve Vim and its genius is doing a perfect job of this.
Let us see for ourselves. We are going to use a C file sqlite2.c for
$ vim sqlite2.c
:hardcopy > %.ps
gets us the file sqlite2.c.ps which is a postscript file. If you open it
with gv you find that it is neatly syntax highlighted. This will only
happen if you have it enabled in your vimrc like this:
se spell spelllang=en_us
noremap :se nospell
Now that is an interesting vimrc. But let us focus on the first line
alone and get on with the article. The other way to do it is by opening
the file in vim and then highlighting the syntax manually with the
colon command ":sy on".
But we are not very happy with this for two reasons. It is in ps format
which is fine for academic papers and not for Internet publishing. And
the second problem with it is that it not suitable for batch
conversions. Hackers are incredibly lazy individuals who want the
computer to work for them. They don't want to sit and do the job.
What is the way out?
It turns out that there are two ways. One way is interesting. The other
a bit boring but both work well.
$ vim -me -e -c ":hardcopy >%.ps" -c ":q" sqlite2.c
works but this is a better way.
$ vim -e -s sqlite2.c < hardcopy.vim
$ cat hardcopy.vim
:hardcopy > %.ps
It is obvious why we prefer the second approach. The first approach
takes a bunch of vim commands. You are limited to 10 and it is clumsy
since you start vim and kill it!
The second approach is filled with UNIX genius. It has redirection and
it is infinitely scalable since your one line vim script can do a lot
more than this job alone.
Please spend an hour studying the tutorial for writing vim scripts by
typing this colon command ":help script"
Vim scripting has object oriented programming, dictionaries arrays and
of course loops.
But can't we do this with the shell?
for file in "foo.c foo.perl bar.c script.sh"
vim -e -s $file < process.vim
Good. Now there is one itch remaining. How to convert the resulting ps
file into PDF?
$ ps2pdf14 sqlite2.c.ps
$ xpdf sqlite2.c.pdf
Happy? No? Then I have another trick for you.
Does looking at this PDF help?
There is a canned plugin that comes with Vim for html conversion. It is
called TOhtml. Invoke it inside a vim session with ":TOhtml"
Now put two and two together and come up with your own way to convert C
files into syntax highlighted html.
For those of you who hate white backgrounds there is a solution. You can
set this in vim.
Got something to add? Send me email.
Increase ad revenue 50-250% with Ezoic
More Articles by Girish Venkatachalam
© 2009-11-07 Girish Venkatachalam