79

I would like to paste my code to a word document while preserving the highlighted, colored texts. Is there a way to do this?

Mariska
  • 1,913
  • 5
  • 26
  • 43

3 Answers3

85

You can install highlight plugin and then use SublimeHighlight: copy to clipboard as RTF command from the command palette.

enter image description here

If you want to use your own color scheme use ExportHTML plugin to export to html and copy and paste the html in MS Word. HTMLExport can be configured to use any of your installed color schemes. just read the docs and look at the default settings.

Use a browser like Safari that can copy in RTF to the clipboard. For me chrome does not work.

Allen Bargi
  • 14,674
  • 9
  • 59
  • 58
  • 5
    If you find your own color scheme use ExportHTML plugin (https://github.com/facelessuser/ExportHtml) to export to html and copy and paste the html in MS Word. HTMLExport can be configured to use any of your installed color schemes. – Allen Bargi Jan 10 '14 at 14:26
  • on my machine this works for HTML/CSS but Javascript looses colors – Jorre Feb 26 '14 at 16:50
  • 2
    If you're using SublimeText3 you need to take the package from https://github.com/n1k0/SublimeHighlight/tree/python3 otherwise your Highligh menu items will be disabled. – Nikita R. Oct 22 '15 at 22:21
82

My Answer here, is for people who Don't know how to install SublimeHighlight Package on Sublime Text 3:

  1. Install Package Control inside sublime text 3. If you don't know how, follow this link here
  2. Open the Sublime Command Palette by pressing CTRL+SHIFT+P` on Windows or ++P on Mac
  3. Type "Add Repository", and press enter

    enter image description here

    Then paste this link https://github.com/n1k0/SublimeHighlight/tree/python3

  4. Press CTRL+SHIFT+P on Windows or ++P on Mac again and type "Install package" and press enter or return

    enter image description here

  5. Type "Sublime highlight" and press enter to install the plugin

    enter image description here

  6. Restart your Sublime

How to Use SublimeHighlight

  • Highlight the Text you want to copy
  • Press CTRL+SHIFT+P
  • Type, for example, "SublimeHighlight: copy to clipboard as RTF: " like so:

    enter image description here

  • Paste it in Word or Powerpoint, you will find the text has been pasted with coloring and formatting.

These are the commands you can use:

  • SublimeHighlight: convert to HTML: will convert current code to highlighted HTML in a new SublimeText tab.

  • SublimeHighlight: convert to RTF: will convert current code to highlighted RTF in a new SublimeText tab.

  • SublimeHighlight: view as HTML: will convert current code to highlighted HTML and open it in your default browser.

  • SublimeHighlight: view as RTF: will convert current code to an RTF document and open the generated file with your default program.

  • SublimeHighlight: copy to clipboard as HTML: will convert current code to highlighted HTML and store it into the system clipboard.

  • SublimeHighlight: copy to clipboard as RTF: will convert current code to raw highlighted RTF and store it into the system clipboard.


Customizing the Theme (OPTIONAL):

  1. go to Preferences --> Package Settings --> SublimeHighlight --> Settings-User.enter image description here
  2. paste this code

    {
    "theme": "fruity",
    "linenos": "inline",
    "noclasses": true,
    "fontface": "Menlo"
     }
    

Here, I have used the "fruity" theme, you can choose whatever you like. These are all possible themes: enter image description here


Set Hotkey (OPTIONAL)

In the next code, I have set the CTRL+ALT+C to copy the text with formatting, you can do that following these few steps:

  1. Go to Preferences --> Key Bindings-User.

    enter image description here

  2. Paste this code:

    { "keys": ["ctrl+alt+c"],
      "command": "sublime_highlight",
      "args": { "target": "clipboard",
                "output_type": "rtf"
    }},
    

    ` Now whenever you copy a text with CTRL+C, it will be copied without formatting and with CTRL+ALT+C, it will be copied with coloring :)

Anwarvic
  • 12,156
  • 4
  • 49
  • 69
  • I did above-denoted things, Also it is working fine, but I need to copy the code with particular line number. Is that possible in Sublime? – Mohanrajan Oct 20 '16 at 08:53
  • @MRKesavan I think this would work [Copy With Line Numbers Reloaded](http://stackoverflow.com/questions/36992564/how-to-copy-text-together-with-the-line-numbers-using-sublime-text) – Anwarvic Oct 24 '16 at 14:12
  • I liked this answer better because it has complete instructions for someone who hasn't used the Sublime Control Panel before. Thanks! – Gaurav Ojha Mar 11 '18 at 20:51
  • Keyboard commands are wrong. For example the command palette is not Ctrl-shift-P, but ⌘⇧P. On the Mac (obviously) – Jean-Denis Muys May 15 '18 at 14:58
  • @Jean-DenisMuys. I edited my answer, thanks man :). Next time when you find something wrong with any post, try to edit it to gain some "reputation". But anyway, thx – Anwarvic May 15 '18 at 15:30
5

The SublimeHighlight plugin does work well! But as Jorre noted, code can lose some colors. I found that the selection you are copying to the clipboard matters.

When, for example, I select a portion of a CSS stylesheet, but the selection does not include the closing style tag, the colors will be lost. It appears that any incomplete element will be copied to the clipboard as white text.

My solution has been to simply copy the entire code file to the clipboard as RTF, paste into Word, then make the selection there for further copy / pasting into a PowerPoint slide or elsewhere.

dishdesigner
  • 51
  • 1
  • 1