Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to email the contents of vim using HTML

Tags:

html

vim

email

diff

I like to view the current differences in the source files I'm working on with a command like:

vim <(svn diff -dub)

What I'd really like to be able to do is to email that colorized diff. I know vim can export HTML with the :TOhtml, but how do I pipeline this output into an html email? Ideally. i'd like to be able to send an html diff with a single shell script command.

like image 791
brianegge Avatar asked Apr 01 '10 14:04

brianegge


1 Answers

The following one-liner produces an HTML file named email.html:

diff file1 file2 | vim - +TOhtml '+w email.html' '+qall!'

You can now use Pekka’s code to send the email.

However, I believe in using the right tool for the right job – and VIM may not be the right tool here. Other highlighters exist and their use is more appropriate here.

For example, Pygments can be harnessed to produce the same result, much more efficiently and hassle-free:

diff -u report.log .report.log | pygmentize -l diff -f html > email.html

Notice that this produces only the actual text body, not the style sheet, nor the surrounding HTML scaffold. This must be added separately but that’s not difficult either. Here’s a complete bash script to produce a valid minimal HTML file:

echo '<!DOCTYPE html><html><head><title>No title</title><style>' > email.html
pygmentize -S default -f html >> email.html
echo '</style></head><body>' >> email.html 
diff -u report.log .report.log | pygmentize -l diff -f html >> email.html 
echo '</body></html>' >> email.html 

EDIT In case that Pekka’s code didn’t work – as for me – because you don’t have the required versions of mail and mutt installed then you can use sendmail as follows to send the HTML email:

( echo 'To: [email protected]'
  echo 'Content-Type: text/html'
  echo 'Subject: test'
  echo ''
  cat email.html ) | sendmail -t

It’s important to leave an empty line between the header and the body of the email. Also, notice that it’s of course unnecessary to create the temporary file email.html. Just paste the rest of the commands into the right place above and delete the redirects to file.

like image 66
Konrad Rudolph Avatar answered Oct 02 '22 14:10

Konrad Rudolph