How to send rich text to the clipboard from command line

clipboardhtml

How can I send html to the clipboard as rich text from a script? My end goal is create a script so I can paste content from a source file into an email, but I want a general answer for pasting into any program that accepts rich text.

Example usage for pasting into email:

  1. Open the source file in vim
  2. Use :TOhtml command to create an html file with vim's syntax highlighting
  3. Use an answer from here to copy the html as rich text
  4. Paste into an email (this one wouldn't be scripted)

Related: Pasting diff output into Microsoft Outlook with syntax highlighting

Related: Copy markdown input to the clipboard as rich text

Best Answer

Linux

via this answer

cat text.html | xclip -t text/html

Mac

via this answer

cat text.html | textutil -stdin -format html -convert rtf -stdout | pbcopy

Windows

In older Windows, you can natively only copy plaintext (via this answer).

type text.html | clip

In PowerShell you can copy rich text:

type text.html | Set-Clipboard -AsHtml

If you create a C:\sandbox\pbcopy.ps1:

type $args[0] | Set-Clipboard -AsHtml

Then you can enable scripts and then run it from anywhere (cmd.exe, .bat files, etc):

powershell C:\sandbox\pbcopy.ps1 text.html

There are a few different Cygwin commands to copy to Windows clipboard and it looks like cygwin provides xclip, so you could probably use the Linux solution on Windows if you have cygwin.

Related Question