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, 2015 at 14:41
  • Expected output would be html (browser friendly format).
    – kenorb
    Feb 14, 2015 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, 2015 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, 2015 at 12:22

2 Answers 2


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


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


  • 2
    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. Feb 15, 2015 at 0:14
  • This doesn't work properly for me on Vim 8.2 (nonstandard file type, not C): vim -E -s -c "let g:html_no_progress=1" -c "set background=light" -c "set ft=myfiletype" -c "runtime syntax/2html.vim" -c wqa myfile.txt. Specifically, the syntax highlighting CSS rules don't get inserted into the resulting HTML file. If I remove the -s for silent mode, it works fine (but not sure if that would work properly without a tty). Nov 11, 2020 at 3:39

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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