I would like to be able to easily create html-colored versions of a list of files (ex: "all the files in this directory", or "this list of files")

I know that I can do this manually, one by one, by:

  1. opening the file (ex: :e /path/to/Foo.bash) in vim (with syntax enabled)
  2. typing :hardcopy to obtain a html-version of the file that also contains the syntax highlighting colors
  3. saving that file (ex: :w /path/to/Foo.bash.htm)

But how can I "automate" this, ie do it for either all files in a directory or for a list of comma separated file names ?

  • I will need to edit my question as I mixed html version and the command to just print ^^ I need the html versions... Commented Sep 22, 2016 at 10:36

1 Answer 1


There is a script named 2html that comes with recent versions of Vim. Its purpose is to convert files to HTML (see :h 2html.vim). You can run it in a batch like this:

  • open all files you want to convert:

    vim *.sh
  • run the script with a bufdo:

    :bufdo runtime! syntax/2html.vim | wq

This should create a file foo.sh.html for each input file foo.sh. Beware that the script converts files in the exact state they are in Vim: the same color theme is used, closed folders are kept closed etc.

  • thanks, i ll try that shorty but it is probably what I needed (and very flexible as I can use the command line to give the list of files, and have the .htm is each respective directories!) Commented Sep 22, 2016 at 10:35
  • works like a charm! (I just need to understand why it saves under the ".html" name instead of the original ones like "wq" should do ^^ .... the 2html.vim script also changes the "current filename" ? neat! Commented Sep 23, 2016 at 14:28
  • a side effect: if I do vim thisdir/* as step 1, when it saves the .html versions they get added to the list to work on, so it loops ! ( ex: if there are 2 files (A.sh, B.pl) it does them but they go on to "A.sh.html", then "B.pl.html", etc... Commented Sep 23, 2016 at 14:33
  • Which is why you shouldn't do that. This is what :h :bufdo has to say about it: {cmd} must not delete buffers or add buffers to the buffer list. The initial command is not adding to the buffer list because the new files <mumble>.html don't match *.sh. So, careful there. Commented Sep 23, 2016 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.