15

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

3 Answers 3

22

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.

2
  • 2
    With Windows you can put rich text to clipboard using cli. Just use following PowerShell command: Echo "<p>foo <strong>bar</strong></p>" | Set-Clipboard -AsHtml Apr 21, 2017 at 23:31
  • -AsHtml doesn't work for pasting to google docs (plain text appears), and there's no option -AsRtf. xclip requires X windows to be installed :-( Mar 15, 2019 at 13:39
2

Also a Pandoc Solution

Developing on the answers here

Alternative 1

  • Use :TOhtml, this will give you a new buffer with the converted html. Next, use :w ! xclip -t text/html -selection clipboard

  • When I pasted this in libreoffice, it had the line numbers. I tried disabling them, and repeating. This worked nicely.

Solution using Pandoc:

  • I prefer this, has a better formatting, and it is a one-liner

    :w ! pandoc -s -t html | xclip -t text/html -selection clipboard

Some explaining:

  • :w ! {cmd} will pipe the buffer to {cmd} in the shell command
  • pandoc -s -t html will take the input and convert to html. I think you can omit the "-t html"
  • "|" works as a pipe, since it's being interpreted as a shell command
  • xclip -t text/html -selection clipboard is the answer given in the link linux answer

EDIT: Trying to assign the command to a keybinding didn't work. It seems like the pipe is being used in the usual vim sense.

My workaround was to define a function:

function Html()
    let mytext = execute('w ! pandoc -s -t html | xclip -t text/html -selection clipboard')
    return mytext
endfunction

And then assigning a keybind to call this function:

nnoremap <leader>h :call Html()<cr>

Hope this helps. If anyone has a simpler solution, please comment!

0

Use e-mail CSS! Use UTF8 encoding html file!

Run http (HTML-eMail.html) e-mail from comman line:

powershell .\mail-http.ps1

mail-http.ps1:

$time = get-date 

$from    = 'So.From@gmail.com'
$to      = 'So.To@gmail.com'
$subject = 'eMail-HTML ' + $time

$server=smtp.gmail.com;$port=587

$encoding = [System.Text.Encoding]::UTF8

$email=new-object Net.Mail.MailMessage($from, $to, $subject, $body)
$email.DeliveryNotificationOptions=[System.Net.Mail.DeliveryNotificationOptions]::Delay
$email.IsBodyHtml = $true
$email.Priority = [System.Net.Mail.MailPriority]::High

$email.BodyEncoding=$encoding

$email.Body = gc '.\HTML-eMail.html' -encoding UTF8

$smtp=new-object Net.Mail.SmtpClient($server,$port)
$smtp.EnableSSL = $true
$smtp.Timeout = 30000  #ms
$smtp.Credentials=New-Object System.Net.NetworkCredential($from, 'derParol'); 

$smtp.Send($email)

HTML-eMail.html:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">

...

</html>
1
  • That only works if I want to compose an email in html. I may be replying to an existing email, I may want to paste into a word processor, etc. I've edited the question to clarify that email was an example, and not the real end goal.
    – idbrii
    May 11, 2015 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.