5

Want a simple way, like notepad++ plugin: NppExport - Export to RTF, to copy selected text with syntax highlighting.

Notepad++, NppExport, Export to RTF example:

  1. Select text
    enter image description here enter image description here

  2. Copy RTF
    enter image description here

  3. Paste with syntax highlighting
    enter image description here

  • 1
    You should explain what you want assuming nobody knows it beforehand. Specifically, tell us what NppExport does. – klaus Jun 26 '18 at 14:08
7

It's a bit roundabout, but one way to achieve this with built-in tools is to use the :TOhtml command to create an HTML file that contains the highlighted text, open that file in a browser, and then copy from there.

:TOhtml takes a range, so you can use this method to "copy" the highlighted contents of a visual selection.

See :help :TOhtml and :help 2html for details.

  • Yes, I tried :TOhtml. It needs to save to another other file, open browser and copy. It will also convert the line number if it's enabled and if paste to outlook, line break gets wrong. I want to directly copy from vim. – Fisher Jun 27 '18 at 7:49
2

I found an email discussion about the same topic: http://vim.1045645.n5.nabble.com/convert-syntax-highlighted-text-to-RTF-td1177362.html

They also suggest :TOhtml and also an alternative plugin to convert to html. And then they suggest pandoc to convert html → rtf.

And also found a vim plugin that claims to export to rtf: https://github.com/zerowidth/vim-copy-as-rtf

It claims to be OSX only but maybe you have that or can fix the code to make it work for you.

1

I'm not on windows, but @root described in his answer, that you can copy formatted HTML to clipboard with the command scb -as (that's what I understood).

The following just collects @roots steps in a function. Put the following in your vimrc:

function! CopyFormatted(line1, line2)
    execute a:line1 . "," . a:line2 . "TOhtml"
    %yank *
    !start /min powershell -noprofile "gcb | scb -as"
    bwipeout!
endfunction

command! -range=% HtmlClip silent call CopyFormatted(<line1>,<line2>)

Then restart Vim and execute:

:HtmlClip
  • What are a:line1 and a:line2? Does this pass particular lines from a file as input to the function? – root Feb 21 at 18:03
  • @root Yes. Default is 1 and last line number. If you visually select a few lines, then its that line number range. – Ralf Feb 21 at 18:05
  • I must be missing something. I've put the function in my _vimrc and reloaded gvim. After, I open a document that has syntax highlighting, I run :command! -range=% HtmlClip silent call CopyFormatted(<line1>,<line2>), and nothing happens. No change if I remove silent. – root Feb 21 at 18:22
  • @root See update. The :command... also belongs into vimrc. You then have to run :HtmlClip. – Ralf Feb 21 at 18:31
  • Nice, this works. The only change is you must replace the single quotes with double quotes around the Powershell input ("gcb | scb -as"). The only drawback I see with this is also present in my solution, in that you must wait several seconds for PS to load before it can process the input. I was hoping -noprofile would help but my machine incurs a 1-3 sec load time. – root Feb 21 at 18:37
0

I am using the following in gVim on Windows 10:

Record a macro that does the following:

  1. :TOhtml
  2. ggVG (select all)
  3. "*y (yank to system clipboard)
  4. :q! (close the TOhtml file)
  5. :!start /min powershell -noprofile "gcb | scb -as" Start a minimized instance of Powershell that runs a shorthand equivallent of Get-Clipboard | Set-Clipboard -AsHtml)

After running this macro and allowing Powershell to complete, I can then paste directly into Outlook 2016.

  • Could you test the command I proposed in my answer? Maybe this is useful for you. I have no Windows machine available to test. – Ralf Feb 20 at 20:09
  • Nothing happens when I run that command. There are no messages and there is nothing sent to my system clipboard nor vim registers. I'll need to do some research on executing Powershell from within gvim. – root Feb 21 at 13:36
  • I've updated my post. – Ralf Feb 21 at 16:48

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.