How to send rich text to the clipboard from command line?
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.
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!