Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 155 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

I might just be lazy, but do you ever find it tiresome to select all the source code from a large post on SO to copy it? I would like a feature, possibly a Firefox extension but preferably a JavaScript script, for automatically copying source examples to the clipboard. It's no big deal for a small snippet, but a 1000+ line posting is cumbersome to copy.

Do you guys have a better way of copying large source posts? I'm using FireFox 3.5.5 on Win7.

share|improve this question

migrated from Dec 10 '09 at 22:30

This question came from our site for professional and enthusiast programmers.

Even copying 25 lines of code can be annoying sometimes. A simple "Copy" link would be really useful. – Noam Gal Jul 18 '10 at 14:36
Cut/paste versus copy button - I'll take a copy button, hands down, for a number of reasons. – IAbstract Dec 9 '10 at 12:57
no wonder feedback is low - retagged with [feature-request]. – IAbstract Dec 9 '10 at 13:08
Related: “Unformatting” a code block which uses a Greasemonkey script ("pre select") to copy the contents of a pre block. – Arjan Dec 9 '10 at 16:30
See also File attachments – bobobobo Dec 9 '10 at 16:47
When displaying code, it seems it's becoming more and more common on other sites to present the user with a quick option (button) that copies all the code in the code block. I don't understand the reason why stackexchange sites would not also implement this feature? – mg1075 Jul 9 '11 at 19:04
Getting close to 2 years that this feature-request was submitted - is anyone even looking at this? – IAbstract Oct 6 '11 at 16:18
On top of this, when you do finally select all and copy it, the code you paste is all unformatted and appear in one line. Is there a way to copy the formatted code with all the spacing? – Jay Dec 14 '11 at 18:01
It's not the copying which is a pain, it's the selecting. I've seen a "select" or "select all" button in many other sites which involve source codes, such as JavaScript minifiers and I think either or both of jsfiddle and the YQL console. Scroll-selecting sucks, and it sucks worse on a netbook )-: – hippietrail Aug 12 '12 at 7:17
So was this idea taken in consideration for future performing? – fedorqui Feb 7 '13 at 9:39
I love this idea! But why just one button? I've suggested a list of possible buttons: – TheBronx Feb 26 '13 at 13:01
@PeterAjtai 2 whole extra seconds while answering questions is a quite reasonable argument – X.Jacobs Mar 24 '13 at 11:12
OP - we all might just be lazy too, that -however- doesn't mean this isn't a brilliant idea ;-) – Adel Mar 24 '13 at 23:10
@IAbstract Well Steve left, doesn't look like he's still waiting for an answer (and I am firmly against implementing said proposal) – bobobobo Mar 25 '13 at 1:45
@X.Jacobs: No. No, it's not. I wish SO users would stop being so damned lazy. It spills over into other aspects of their life, such as eating, and doing prior research on questions. – Lightness Races in Orbit Jul 18 '13 at 9:21

15 Answers 15

If there is so much source code in a question that it is onerous to copy it, and especially that you feel a need to paste it all in your response, then both the questioner and you the answerer are doing things wrong. There is rarely a need for a huge amount of source, and you shouldn't have to repeat it in your response. If you do, I figure you deserve to go through a few extra steps to do so. It's not something we want to encourage.

share|improve this answer
I shudder to think that he's not copying a huge block of code to paste it into a response, but into his IDE. – Bill the Lizard Sep 17 '10 at 15:38
I don't think the primary purpose of the copy button is to simply copy question code blocks to a response. It would be for copying code snippets to clipboard and pasted in an editor. – IAbstract Dec 9 '10 at 13:11

Do you guys have better way of copying large source post?

Edit -> Select all -> Copy -> Paste -> Edit out non-source portions.

But then, the only time I'm copying 1000+ line posts is to edit them anyway. That's really a bit much for SO...

share|improve this answer
I want to respond this:… You misunderstood what I mean in my request to implement a "COPY to Clipboard" button. – xport Dec 23 '10 at 22:34
The purpose is to ease testing process when somebody answers with code inclusion. It is very useful in There we intensively provide minimal code snippet for testing purpose. For the production, adjustment might be needed. – xport Dec 23 '10 at 22:41
EDIT->Select All ->Copy -> Paste will not work because if you choose Select All, the whole page get highlighted. You need to copy the whole page including the banner, related links, and other unnecessary things? – xport Dec 23 '10 at 22:43
@xport: the point of the edit step is to allow you to select all within the textbox - that is, just the text of the post itself, not the page chrome. And again, if someone's posting 1K LoC, there is no "easy" solution - that's quite a few pages of code, far too much to even comprehend without at least a bit of time invested. Worrying about a few extra clicks to get it into your clipboard seems a bit like complaining that you have to walk all the way to the end of the pier before swimming across the Atlantic... – Shog9 Dec 23 '10 at 23:52
How to SELECT ALL a single code snippet region (or textbox in your terminology) that a person submit in his/her answer? – xport Dec 24 '10 at 0:42
Maybe time to reject this poor feature request then and bring it to eternal rest? :) – Shadow Wizard Nov 19 '13 at 7:54

There are some cases in which all the code is in one line and is very long like answers to this post. This lines are hard to copy and it would be nice to have a Copy code button

share|improve this answer
CTRL+C works fine. I believe the line breaks must have been added by the original poster, because by default you will get a horizontal scrollbar instead. – bobobobo Dec 9 '10 at 16:50
Why add breaks? You should be able to copy from the IDE and have the line breaks added automatically or propertly formatted or if it is no posible then the line breaks are ok. Similarly you should be able to copy code from an answer or question in an easy way. – Carlos Muñoz Dec 11 '10 at 5:19
@bobobobo: CTRL+C might work fine, the issue is with the selection of the code sample block; not always easy to get right, especially with long code blocks. – casperOne Aug 11 '11 at 13:14
If you're finding it difficult to select an entire line because it's so long, just triple click and it'll select the entire line. – JBurace May 30 '12 at 14:32

I can't understand why people would object to taking one single click (to select all and copy) instead of many steps (as Shog9 suggested).

And w.r.t. Ether's answer, I don't think it matters how many lines of code you want to copy. I'd say it's particularly painful if there are only a few lines.

I've run into many sites with "copy code" button. phpBB has Select All.

share|improve this answer
Selecting a block of text and hitting control/command-C is "painful"? – Ether Sep 17 '10 at 16:22
Infinitely more painful than hitting one "Copy Code" button? YES. More painful than being stuck at 11 rep? NO. – Geoffrey Zheng Sep 22 '10 at 14:14

Considering all of the source code is wrapped around a < pre>< code> block, I suspect it would be easy enough to write a Greasemonkey snippet to find those code block and copy them to the clipboard for you.

Or similarly, I've been using EverNote as of late and find that hilighting the code in a question/answer, then hitting CTRL+ALT+V will grab the selected text and put in into a new note entry for me. Then I can easily take it where I need it.

share|improve this answer
As you have stated it is easy enough. Easy enough for the dev team at stackexchange to implement it. – Carlos Muñoz Sep 26 '10 at 18:02
It's not that easy, because unless you get to it before the prettyprint script, the code as a simple block of text is no longer there... – Benjol Dec 9 '10 at 13:43
See “Unformatting” a code block which uses a Greasemonkey script ("pre select") to copy the contents of a pre block, but shows that this needs some tweaking to get rid of the Google Prettify stuff. – Arjan Dec 9 '10 at 16:31
up vote 102 down vote

I think something along these lines would be an excellent option:

a copy button above right corner in code block

Having a copy button means:

  • I get all the text in the code block
  • I don't have to click/drag the full length of the text
  • Automatically copied to clipboard

I don't know why people would be against this small piece of added functionality. It makes sense to have a copy button. I certainly don't expect this comfort control to take priority but an official word on whether it will be considered would be nice.

Update & Alternative
I have learned that this functionality simply will not be implemented due to security concerns.

An alternative that I would not mind having would be an "Email me teh codez" button. One click, done! Now I have a somewhat permanent record of the code which I can organize in mail folders and copy at any time.

share|improve this answer
I must be honest, the button is so 90's, but the idea is good. – Camilo Martin Feb 5 '12 at 14:51
I couldn't agree more ...but there is a certain convenience about it. – IAbstract Feb 5 '12 at 23:09
Copying code (especially long ones) from SO is a pain the ass!!! – Shimmy Jan 1 '13 at 4:42
The button could appear only if the mouse hovers near the right-top of a code block. By the way, I'm not sure if it's technically possible to place code on the clipboard in a clean way. As I remember, you had to abuse a Flash control, which would be a no-go for any sane website. – Andomar Feb 7 '13 at 10:27
ZeroClipboard seems to do a nice job of abstracting the actual "copy to clipboard" code. – ThinkingStiff Feb 25 '13 at 22:52
I agree with Camilo Martin lol! It's definitely possible with pure js @Andomar as Syntax Highlighter does it: – Adam B Feb 26 '13 at 5:24
FWIW, I agree. We were just having a discussion in about the pros and cons of adding various plugin features. The proposer noted that has them -- but it starts with the "copy" feature. In short this might open a can of worms but that could be powerful/cool. – Joe Corneli Mar 7 '13 at 0:06
@Shimmy: There shouldn't be any "long ones". – Lightness Races in Orbit Jul 18 '13 at 9:22
@LightnessRacesinOrbit, U got my vote, but I said sometimes. There are those who paste in huge code with no source. – Shimmy Jul 19 '13 at 12:49
@Shimmy: Those posts should be downvoted and then deleted. – Lightness Races in Orbit Jul 19 '13 at 12:52
I completely disagree. Sometimes they are useful, just not formatted the right way. – Shimmy Jul 19 '13 at 15:49
I want this feature really. – puretppc Jan 25 '14 at 5:01
What kind of security concerns? Any source for that? – Stijn Jul 4 '14 at 14:11
@Stijn: since the copy functionality must copy to the client clipboard, there are security concerns. I can't recall the details that I was given specifically. – IAbstract Jul 5 '14 at 6:49
Here is a Greasemonkey script to implement a prototype of this feature. On Chrome, I believe you need to have Tampermonkey installed. Click the Raw link to install it through TamperMonkey or GreaseMonkey. This script will tweak all pages to add the copy link on hover. – Ross Rogers Jun 3 '15 at 18:56

Nah. Jeff specifically said somewhere on his blog that SO was not for posting "huge swathes of code." And how.

What code blocks should be is 1. easy to highlight with the mouse and CTRL+C, which they are. As you can see:

Code code code
Oh more code

The other part is 2: when you paste it somewhere, annoying line numbers don't spill into your code which so far, doesn't happen.

I'd say SO has got codeblocks right, and they shouldn't be touched. I'd shudder if they ever look like the code blocks here.

I've always hated the "copy" button on sites that sites have, mostly for the reason if you can write to my clipboard, you also read it. And people don't usually like that. See the need for Flash to do this.

share|improve this answer
I would hope that line numbers would not be inserted. In addition, does MSDN use flash to copy code snippets from it's site? I don't believe Flash is the only way for this to be worked out. – IAbstract Dec 9 '10 at 17:50
The above example works but copying and pasting doesn't work with context colored code samples in IE8. It does work in Chrome. In IE8 the samples are pasted all in one line. See this code sample… – Ed Greaves Jun 2 '11 at 14:48
I actually love the code blocks you linked, shudderingly, to :) – sehe Jul 4 '11 at 13:45
can't find ctrl-c on my iPad... :( – cormullion Aug 19 '12 at 9:54
Hold your finger down and select "Copy". – bobobobo Oct 8 '12 at 15:07

I'd like to see the copy feature as well. I do more answering than questioning - and a lot of the PHP questions that I look at are written by newer users. They aren't familiar with the culture and have a tendency to post blocks of code followed by "what did I do wrong?" For these scenarios, it's often much easier to copy into an IDE and take a look at it the way I would look at my own code. (Especially if the lines extend past the codeblock width.)

SO shouldn't be a snippet site, but it is devoted to helping coders share knowledge. Such a feature facilitates that transaction.

share|improve this answer

I don't understand people's aversion to usability. (Looking over some of the Q&A on MSO, it seems like some people have an aversion to any kind of change.)

Code snippets don't have to be 1000+ lines to create a vertical scrollbar. Worse still are code snippets on one long line with a horizontal scrollbar. Those are a real pain to copy.

How can anything that makes things a little easier be a bad thing? Today, it should be fairly simple to implement in jQuery. SE already uses jQeury anyway. I think it's worth looking into.

share|improve this answer
The thing is that you would have to add the clutter of an additional UI element for relatively little gain. As long as copying code is just a bit hard (and not impossible, say, through line numbers) I'm opposed to adding this – Pëkka Sep 16 '11 at 10:01
But it's a fairly standard UI element for code snippits. I'd hardly call it clutter. – Herbert Oct 12 '11 at 9:01
It would be no more clutter than the Share|Edit|Flag links. This is such as basic and useful feature that is very standard on many coding sites and should be there on Stack Overflow. – martinjbaker Feb 15 '13 at 12:48
@Herbert: Changing software is not the default action. It's not a general aversion to any change -- it's that you need to present a solid use case for spending expensive man hours implementing a new software feature, and that has not been done here. – Lightness Races in Orbit Jul 18 '13 at 9:24
Copying and pasting code out of answers is an uncommon scenario. And in the case it's not, we'd like to discourage copy-and-paste programming. – Cody Gray Aug 4 '13 at 10:29
@LightnessRacesinOrbit I disagree about man hours, that's ridiculous. The number of man hours that were spent on less useful SO features go way beyond what it would take to add this. When a suggestion gets 148 upvotes on a meta site, and you still see some categorical yet unexplained "no"s, that's called stubbornness. – Schollii Apr 24 '14 at 5:19
@Schollii: Not everybody agrees with you that this should be done. "Less useful SO features" is subjective and debatable. – Lightness Races in Orbit Apr 24 '14 at 9:19

Simple and unobtrusive. ZeroClipboard and sytaxhighlighter both seem to do a nice job of abstracting the actual "copy to clipboard" code.

copy box

share|improve this answer

What about something along the lines of how allow users to open code in writeLaTeX (an online collaborative LaTeX editor/compiler)? For details and examples of how it works, see the announcement:

This type of integration has been proposed for TeX SX, and with the recent integration of CircuitLabs into Electronics SX, it seems like there are some precedents (and models) for how this could work.

share|improve this answer
I should add that I'm one of the developers at writeLaTeX, so happy to discuss how we could work on an integration along these lines for SX – John Hammersley Mar 7 '13 at 0:19

I really would like this feature, and it would make it easy to test out code.

Like today, I wanted to copy the following:

enter image description here

share|improve this answer

Select code block buttons.

H.B. on StackApps has written a script to do exactly this.

A user-installable script let's anyone who wants this feature have it without running afoul of any of the objections raised in other answers.

See also this question for some further discussion of similar tools.

share|improve this answer

If you are struggling to select all of the code in a question/answer then the correct action is to EDIT THE POST to remove the extraneous code. There should never be so much code in a post as to pose a useability problem.

Adding auto-copy functionality will promote behemothic walls of crappy code that we simply do not want.

share|improve this answer
@javadba: What a vacuous counter-argument. It appears that you did not understand this answer. – Lightness Races in Orbit Aug 8 '15 at 15:06
@javadba: I might consider doing so when you cease the hostility. – Lightness Races in Orbit Aug 8 '15 at 16:55

enter image description here

I implemented a Chrome extension which solves all these problems.
Clipboardy is a Chrome extension for copying posted code to clipboard from, & It can:

  • Copy the source code to clipboard with just one click
  • Select the source text
  • Collapse the source text
  • Show the history of clipboard usage
  • Configurable toolbar

Install the extension from chrome store or read the product page.
The app is open source, see the repository. Feel free to report issues or improvement ideas.

share|improve this answer

You must log in to answer this question.