13

I've a file of source code written in a programming language (e.g. PHP) and I would like to convert it into a HTML file, so I can publish it on web in order to share my code.

By conversion I mean, for example, converting new lines into <br> tags so that the text will keep the same formatting both in the text editor and in the web browser. Ideally it should also preserve syntax highlighting so it can be also printed.

Is this achievable in Vim? If so, how?

  • 4
    What do you mean by converting? What is expected output? – OrangeTux Feb 14 '15 at 14:41
  • Expected output would be html (browser friendly format). – kenorb Feb 14 '15 at 14:42
  • 1
    All text files are 'browser friendly'. But when I see Alexander Myshov's answer I think I understand what you mean. – OrangeTux Feb 14 '15 at 14:45
  • 1
    I think a detail is missing from the question. Do you want to produce a full HTML document for each source file or just a fragment of HTML code you can embed in another document, or it doesn't matter? – toro2k Feb 16 '15 at 12:22
14

The following vim command would creates an html rendering of the current file.

:TOhtml

It saves the file in the same folder (with .html extension) and it will include styles, foreground/background colours and syntax highlighting, so the file can be straight web published as well as printed.

For more options (like adding line numbers, compability with old browsers, etc.), check: :help TOhtml.

To convert file non-interactively, try the following command:

vim -E -s -c "let g:html_no_progress=1" -c "syntax on" -c "set ft=c" -c "runtime syntax/2html.vim" -cwqa myfile.c

Related:

  • 1
    You don't need the %, since that's the default. Also note that this is exactly the same as the other answer (:runtime! syntax/2html.vim), the only difference is that with TOhtml you can add easily a range (instead of setting g:html_start_line and g:html_end_line manually. – Martin Tournoij Feb 15 '15 at 0:14
5

As I understood you, you want to convert content of current window to HTML. Try to run this command:

:runtime! syntax/2html.vim

more info here:

:help convert-to-HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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