Vim Tips Wiki
Advertisement
Tip 198 Printable Monobook Previous Next

created January 15, 2002 · complexity basic · author Hari Krishna Dara · version 5.7


When sending code snippets or diffs to your colleagues either for code review or for something else as email, how nice and clear it will be if you can paste it with the Vim syntax highlighting? I am sure they will be impressed and feel much easier to read the code. It is also very easy and fast (once you practice it) to do this.

This probably works only on windows and requires you to use Internet Explorer and an email client that understand RTF content coming from clipboard, such as Outlook or Outlook Express. At least that would make the process faster. I haven't tried on any other combination though. This is what you need to do:

  • Open the file containing the code/code snippet/diff etc. in gvim. If you use dark background for GVim (like me), then I would suggest you to change your color scheme temporarily to something else that has a white background or just use the "-U NONE" as below:
gvim -U NONE <file>
  • Convert the file into HTML by using the following command at the colon prompt as below:
:runtime syntax/2html.vim
  • The above step will open a new window with the HTML content in it. You might want to just save it with the suggested name or write into a temporary file as:
:w! c:/tmp/t.html
  • Open the IE browser window and open the above temp file "c:/tmp/t.html".
  • Now you select all (press ^A) and copy it (^C).
  • You are ready to paste it with syntax coloring in any application that accepts RTF content from clipboard, including Outlook or Outlook Express mail composing window.

Comments

If you are using gvim then you can convert to HTML from the Syntax menu, so you do not have to remember the right file to source.

I got it to work using Netscape. I converted to HTML, saved the file, opened the file in Netscape, and chose "Send Page..." from the file menu. This sends the HTML as an attachment. When I got the message, the attachment was displayed in-line; this depends on how preferences are set. It would probably also work to simply save the file and then attach it.


For novice users (or those who don't seem to remeber all those tricks) gVim 6.x has menu item under "syntax->ConvertToHtml" it will produce a filename.html with coloring. Use this html as you hearts desire!


Advertisement