I'm attempting to copy and then paste code examples from existing Stack Overflow posts into personal documentation which I often end up using Word to paste into. I find that I often am having to copy from Stack Overflow and paste into Visual Studio or some other third-party software to get a pretty-print version of the code as copying from Stack Overflow does not retain the formatting.

Is there a way to retain the formatting?

NOTE: I have copied formatted text from countless number of websites (Ctrl + C/Ctrl + V) and all of the formatting is retained... how does it work for those websites, but not for Stack Overflow specifically? I am not talking about web sites that provide a nice copy button, but simply using your keyboard.

Enter image description here


1 Answer 1


While it sounds nice and simple, it's not on Stack Exchange side, actually.

Stack Exchange is using highlight.js library to make those nice colors you see. That library parse the code blocks, and assigning the proper CSS to each based on the language.

When you copy a code block, you copy only the raw text. It's technically almost impossible to copy the code block along with the CSS that is applied to it.

What you can do though, is install plugins for your IDE, e.g. highlight.js plugin for Visual Studio. Then when you paste the code from SO, it will have the same formatting.

  • I think OP is looking for colors in MS word. They use VS as a workaround to color their code because they can’t paste it with colors directly from the site for some reason.
    – Laurel
    Commented Nov 12, 2020 at 16:03
  • 1
    That's the understanding I was looking for. A 100% solution would have been great but the work around of using a highlight.js plugin is 85% or 90% imho since there isn't a direct method to copy the CSS formatted result of the text. Also... I could not post the question without adding the tag 'feature-request' for whatever reason. I would not have added it otherwise. Commented Nov 12, 2020 at 21:40
  • @Shadow I have however copied formatted text from countless number of websites (ctrl+c/ctrl+v) and ALL of the formatting is retained... I don't suppose you have any knowledge as to how it works for those websites but not for Stackoverflow specifically? Commented Nov 12, 2020 at 21:42
  • @Shadow An example of a web page where I am able to copy/paste and the formatting as seen on the web page remains intact is: stackexchange.github.io/StackExchange.Redis/ThreadTheft.html. I am able to copy the code within the code blocks into MS Word and the formatting comes with. I'm still not sure why it does not do this on StackOverflow. Commented Feb 1, 2023 at 21:22
  • ^ the website I mention in my last comment is also using highlight.js and it works to copy/paste. So... I am not able to understand how this is on My End when copying text from StackExchange works and doing the same on StackOverflow does not? Commented Feb 1, 2023 at 22:50

You must log in to answer this question.

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