Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Using vim for highlighting in Perl

by Hrunting (Pilgrim)
on Mar 26, 2003 at 16:16 UTC ( #245976=perlquestion: print w/replies, xml ) Need Help??

Hrunting has asked for the wisdom of the Perl Monks concerning the following question:

I have a lot of different files that I want to display in highlighted form on a web page. These are different file types, ranging from Perl to config files to Java, etc. vim has an excellent syntax highlight engine, so it seems like a great choice to be able to do this kind of work. Has anyone tried to integrate vim within a Perl script?

Here are the possible options. First, one could parse the vim syntax files and try to write a parser for them in Perl (probably the best way to do that is to deconstruct the C source to vim). Second, one could figure out how to take the vim output of a file with the color-control characters and map them to whatever output method is needed (HTML, XML, terminal, etc.). Finally, perhaps there's some sort of method to plug into the engine directly (doubtful).

Anyone ever try this?

Replies are listed 'Best First'.
(jeffa) Re: Using vim for highlighting in Perl
by jeffa (Bishop) on Mar 26, 2003 at 16:22 UTC
    I have not tried this simply because we already have perltidy:
    perltidy -html -css=mystyle.css
    Perltidy rocks. See also Perl::Tidy, which is the actual 'guts' of perltidy.

    Ooooops ... i glanced over "ranging from Perl to config files to Java, etc." Hmmmm, i do know that gvim has a menu command Syntax->Convert to HTML, but i do not know how to access this via command line. If you can indeed do such, a Cheap Solution® might be to fork and exec a another process. Coding only in Perl is another choice i can think of ... ;)

    UPDATE x 2:
    ndwg++ :)


    (the triplet paradiddle with high-hat)
Re: Using vim for highlighting in Perl
by ndwg (Beadle) on Mar 26, 2003 at 17:15 UTC
    Great idea. I could see how this could be a nice feature to add to some of my projects, so I did a little research and here is what I came up with:

    The relevant reading in vim...

    :help syntax :help 2html

    From the command line...

    vim -f +"syn on" +"so \$VIMRUNTIME/syntax/2html.vim" +"wq" +"q" yourfi
    This will create yourfile.html in the current directory. I had to tweak the resulting HTML to change the background from white to black. (Perhaps since that is what my terminal background color is, that is the colors that vim used.) I would be very interested to know if you come up with any improvements on this method.


      This is pretty close to what I'm looking for. The 2html.vim syntax is pretty easy to figure out, so customizing it to produce the kind of output I desire shouldn't be too difficult. Large files take a good deal of time to colorize, but that's the case in normal vim. The only downsides I can see to this method are that it requires an extra file to be created (it's running in a separate vim window) and it's pretty heavy on the old resources. Wrapping it in a module shouldn't be too difficult, though.
      To make the generated markup c?leaner, as well as much easier to recoulorize, set the html_use_css variable.
      vim -f +'syntax on | let html_use_css = 1 | source $VIMRUNTIME/syntax/ +2html.vim | write | qall"
      Note that it produces the output in, not foo.html. You could also replace the write with a saveas /tmp/foo to have the result always placed in /tmp/foo which may be more convenient for shell scripting.

      Makeshifts last the longest.

Re: Using vim for highlighting in Perl
by traveler (Parson) on Mar 26, 2003 at 17:07 UTC
    To run the html converter from the command line, do
    gvim -c ":so C:\Vim\vim61\syntax\2html.vim"
    replacing the C:\Vim\vim61 with the appropriate dir and with the appropriate file. This will not save the file. I'd copy 2html.vim to a local dir, add the commands to save and exit (:wq! probably) and source that.

    HTH, --traveler

Re: Using vim for highlighting in Perl
by stefp (Vicar) on Mar 26, 2003 at 17:07 UTC
    If if you do :help 2HTML, you will get the to documentation about the conversion of an higlighted buffer to the equivalent HTML.

    2HTML						*2html.vim* *convert-to-HTML*
    This is not a syntax file itself, but a script that converts the current
    window into HTML.  Vim opens a new window in which it builds the HTML file.
    You are not supposed to set the 'filetype' or 'syntax' option to "2html"!
    Source the script to convert the current file: >
    	:runtime! syntax/2html.vim
    	Warning: This is slow!
    After you save the resulting file, you can view it with any HTML viewer, such
    as Netscape.  The colors should be exactly the same as you see them in Vim.
    Read the doc. There is more to it. I did not tried it though.

    -- stefp

Re: Using vim for highlighting in Perl
by tachyon (Chancellor) on Mar 26, 2003 at 22:19 UTC

    You are probably aware that the default perl syntax highlighting in vim explodes on things as simple as  s!https?://!! as it does not believe in regex delimiters other than / # or [ (at least in my version). It is also pretty qq!! blind as well amongst other things. Perltidy does a considerably better job (on perl) as it needs (and has) a much better parser/tokeniser. You have to hack the perl.vim syntax config file to fix some of these issues if it does not work properly for your style of coding.

    Personally I would use vim for generating everything but Perl syntax highlighted HTML as it does a fine job on more rigidly structured code




      The default perl syntax highlighting does break on some things, but it does allow the regex delimiters (!/# etc) you mention (vim 6.1.422, $VIMRUNTIME/syntax/perl.vim 30 Dec 2002). qq!! does not highlight correctly, but qq##, qq() etc do.

      I have emailed the maintainer a patch to handle the ! delimiter in q, qq, qw, qr expressions.

      You can download the latest syntax files from, or for the perl syntax specifically.

        Thanks for that. I have a slightly earlier version of 6.1. Would you be able to post your patch here perhaps? I ma sure I am not the only one who does qq!<var="blah">! and the like so it would have general appeal.

        Speaking of patches have you also patched the pod implementation by chance. This chunk of pod looks rather odd in vim. It is one =head1 and one =cut but confuses vim no end.

Re: Using vim for highlighting in Perl
by ignatz (Vicar) on Mar 26, 2003 at 17:24 UTC
    I'm going to go out on a limb and suggest a *GASP* Python solution. ViewCVS combined with GNU Enscript is what I use for this sort of thing. Import into CVS and BadaBING! If you want the colorized files without all the CVS trappings Enscript can export to HTML so I'd look into maybe an `enscript --language=html >` sort of solution.
Re: Using vim for highlighting in Perl
by Zero_Flop (Pilgrim) on Mar 27, 2003 at 05:47 UTC
    silly rabbit, use perl!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://245976]
Approved by hardburn
Front-paged by particle
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2023-12-03 01:16 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (20 votes). Check out past polls.