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


4 Answers 4



via this answer

cat text.html | xclip -t text/html


via this answer

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


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
    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 Commented 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 :-( Commented Mar 15, 2019 at 13:39

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

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!


Mac, for Google Docs

For Google Docs/Sheets running in Chrome on Mac, you can use this snippet:

#!/usr/bin/env swift

// Based on https://github.com/chbrown/macos-pasteboard/issues/8#issuecomment-906537204
import Cocoa
import Foundation

let pasteboard: NSPasteboard = .general

let dataTypeName : String = "public.html"
let dataType = NSPasteboard.PasteboardType(rawValue: dataTypeName)
var text : String = ""
while let line = readLine() {
    text += line
    text += "\n"
let data = text.data(using: .utf8)!
pasteboard.setData(data,  forType: dataType)
echo '<b>qwerty</b>' | ./pbcopy_html.swift

Note that it would paste only in Google Docs, pasting would fail in other places.

To extract html content from the clipboard, plain pbpaste can be used:

pbpaste -Prefer public.html

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

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

powershell .\mail-http.ps1


$time = get-date 

$from    = '[email protected]'
$to      = '[email protected]'
$subject = 'eMail-HTML ' + $time


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

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


$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'); 



<?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">


  • 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
    Commented May 11, 2015 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .