Pasting code with syntax coloring in emails | Vim Tips Wiki

created 2002 · complexity basic · author Hari Krishna Dara · version 6.0


Many file types can be displayed with syntax highlighting. :help syntax

Vim provides a script that can create an html document (including the foreground and background colors, and syntax highlighting) from the current file, or from selected lines. :help convert-to-HTML

Contents

[show]

Creating htmlEdit

In gvim, use "Convert to HTML" on the Syntax menu. A new file is created, containing the html equivalent of all text from the current buffer.

The same operation can be performed by entering a command:

:TOhtml

If you visually select some lines before entering :TOhtml, only the selected lines will be converted.

If your color scheme uses a dark background, you may want to temporarily switch to a white background before creating the HTML document. Copy-paste of rich text from the browser into an email client or other application often discards the background color. One way to do that is to use the command:

:colorscheme default

Using the HTMLEdit

Of course the HTML file created by :TOhtml can be used on a web site. In addition, on some systems it may be useful to open the HTML file in a web browser for printing. On Windows, you can copy text from within Internet Explorer, and paste it into some other applications in RTF format.

Configuring the outputEdit

The :TOhtml command works by sourcing the $VIMRUNTIME/syntax/2html.vim script, which accepts several options to customize the output. You can ignore folding, add dynamic javascript-based folding to match your document in Vim, change the encoding used, and more. See :help :TOhtml for details. For some options such as dynamic folding and font settings, you may need to grab the latest version of the 2html.vim runtime file. Visit www.vim.org for details on updating all your runtime files or ftp mirrors from which you can download individual files.

The latest version of :TOhtml will make standards-compliant code by default, but for Vim versions before 7.3, it is highly recommended to put :let g:html_use_css = 1 into your .vimrc.

Modifying the HTMLEdit

If you want to paste the generated HTML code into a web forum, often you cannot also paste the CSS generated by default, or prefer not to. Old versions of :TOhtml generate with font tags and the like by default, but you can still use this old behavior in new versions with :let g:html_use_css = 0 before generating code.

When using this option, the HTML generated by :TOhtml places the color of the background and default foreground into the tag <body>. If the code is pasted into an HTML-enabled web forum, those colors disappear.

This function solves the problem, by removing the HTML header and replacing the <body> tag with a <table> instead. This allows you to paste a valid (albeit deprecated) snippet of HTML into the forum with colors fully defined.

  1. function!MyToHtml(line1, line2)
  2. " make sure to generate in the correct format
  3. let old_css = 1
  4. if exists('g:html_use_css')
  5. let old_css = g:html_use_css
  6. endif
  7. let g:html_use_css = 0
  8. " generate anddelete unneeded lines
  9. exec a:line1.','.a:line2.'TOhtml'
  10. %g/<body/normal k$dgg
  11. " convert body to a table
  12. %s/<body\s*\(bgcolor="[^"]*"\)\s*text=\("[^"]*"\)\s*>/<table \1 cellPadding=0><tr><td><font color=\2>/
  13. %s#</body>\(.\|\n\)*</html>#\='</font></td></tr></table>'#i
  14. " restore old setting
  15. let g:html_use_css = old_css
  16. endfunction
  17. command!-range=%MyToHtml:call MyToHtml(<line1>,<line2>)

Now you can select a range of lines, type :MyToHtml and use the result to post your code to an HTML-enabled web forum

 
 

posted on 2017-02-12 16:35  夏威夷的雪人  阅读(279)  评论(0编辑  收藏  举报