tags:

views:

88

answers:

2

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.

+1  A: 

I'm no Linux Guru, but this looks like it should serve your needs to pipe your output into:

Send an HTML file as email from the command line. (uses mail)

There's also a one-line mutt example here:

mutt -e "my_hdr Content-Type: text/html" 
     -s "my subject" you@xxxxxxxxxxx < message.html

this will generate a pure HTML E-Mail with no pure text alternative - for that you would have to build multi-part mail... But maybe it'll do for what you need.

Pekka
Care to explain the downvote? Is there something technically wrong here?
Pekka
+7  A: 

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.

Konrad Rudolph
you have an interesting definition of "more efficiently and hassle-free". +1 for how to get vim to write and quit from the command line.
rampion
you could change the "+w email.html" to `+%!mutt` or whatever to have vim invoke the mail command directly
rampion
@rampion: Launching Vim that way is actually quite inefficient. Pygments easily outperforms it. By hassle-free I simply mean that the Pygments work-flow is conceptually easier (“syntax-highlight a diff”, as opposed to “open a diff in an editor, run a command, close editor”) even though it’s more text to write. Both methods have their merits I think.
Konrad Rudolph
+1 I agree with the fact that vim is not the adequate tool in this situation.
matias