Wouldn't it be cool to right click anywhere in a code block and have the option "copy code block to clipboard"?
3 Answers
I now programmed this Greasemonkey script for Firefox to add a 'select code' text below every pre tag. If clicked the tag content will be selected and the user can copy it using CTRL+C or similar.
This is not a full solution yet (only work with Firefox) but could be expanded to all browsers and included in the site javascript files.
It looks like this:

-
3Please make it work also with Chrome.lockstep– lockstep2011-02-19 16:11:43 +00:00Commented Feb 19, 2011 at 16:11
-
@lockstep: I will have a look on itMartin Scharrer– Martin Scharrer2011-02-19 19:10:52 +00:00Commented Feb 19, 2011 at 19:10
-
I've linked to this answer on meta.gis.stackexchange.com/questions/483/…, where a similar question has come up. The script works there (and I suspect on all stackexchange sites) if you use
// @include http://*.stackexchange.com/*. Maybe you could incorporate that in the version hosted on your server?Jake– Jake2011-05-29 20:47:38 +00:00Commented May 29, 2011 at 20:47 -
@lockstep: Does it work with the current version of Chrome?2012-05-18 08:42:51 +00:00Commented May 18, 2012 at 8:42
-
@lockstep: It works now. But you need tempermonkey user script manager. Refer : How to make `select code` work with google chrome?user11232– user112322013-01-19 13:47:27 +00:00Commented Jan 19, 2013 at 13:47
-
@MartinScharrer -- this question has been cited in meta.tex.stackexchange.com/q/6183/579 but trying to connect to your link fails. can you do something about that, please?barbara beeton– barbara beeton2015-05-31 12:06:28 +00:00Commented May 31, 2015 at 12:06
I have created another Greasemonkey (Firefox) / Tampermonkey (Chrome) script that use clipboard.js.
When you hover a code section (<pre><code>) there is a small </> sign that appears on the top right corner of the script. You click it and voilà !
The code is available on this Gist.
How to use it :
- Install Greasemonkey (Firefox) / Tampermonkey (Chrome) for your browser.
- Read the code in the Gist (if you do not understand some code, do not install it) ! Click on the raw version of the gist.
- Install it.
Edit : I have created a second version. In the first version the button is not visible if the code is scrolled down. In the second version it stays visible ... and it has a different appearance (more \tex one ;)).
-
Does this work for code indented by 4 spaces as well?2015-12-23 20:50:36 +00:00Commented Dec 23, 2015 at 20:50
-
I think yes : code indented by 4 spaces is wrapped in
<pre><code>.Kpym– Kpym2015-12-23 20:53:18 +00:00Commented Dec 23, 2015 at 20:53
October 2025
I have found two solutions:
- The app SOX which contains the option
Add a button to code in posts to let you copy it.
- Code Copier for Google Chrome and Mozilla Firefox.


<pre class="lang-tex prettyprint">Now if I could strip out all the HTML tags, I would have my code block. This is probably a long way round, but does this not show this is possible?