How to send rich text to the clipboard from command line?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping
--
Chapters
00:00 How To Send Rich Text To The Clipboard From Command Line?
00:45 Accepted Answer Score 23
01:37 Answer 2 Score 0
02:22 Answer 3 Score 3
03:59 Answer 4 Score 2
04:37 Thank you
--
Full question
https://superuser.com/questions/912712/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#html #clipboard
#avk47
ACCEPTED ANSWER
Score 23
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.
ANSWER 2
Score 3
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 commandpandoc -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!
ANSWER 3
Score 2
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.clearContents()
pasteboard.setData(data, forType: dataType)
exit(0)
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
ANSWER 4
Score 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>