5

Chrome browser has an excellent extension called autocopy, which copies text whenever some text is selected/highlighted.

To copy a region in emacs, I have to select the region and then press M-w to copy it. But whenever I select a region, I want the region to be copied automatically. How can I achieve that in emacs?

7

The selection can be copied automatically if the OS supports primary selection by setting the below variable.

(setq x-select-enable-primary t)

From C-h v x-select-enable-primary, you get,

Non-nil means cutting and pasting uses the primary selection The existence of a primary selection depends on the underlying GUI you use. E.g. it doesn't exist under MS-Windows.

To learn even more, visit the relevant info node by doing C-h i g (emacs) Primary Selection.


Another variable setting that goes in tandem with the above is to set the x-select-enable-clipboard to t. Doing that allows you to yank the contents copied from other X11 applications into emacs.

(setq x-select-enable-clipboard t)

When emacs 25.1 is released (supposedly the next stable version after 24.5), the x-select-enable-primary variable name will be deprecated and select-enable-primary must be used instead (removal of that x- prefix). Similarly x-select-enable-clipboard will be deprecated in favor of select-enable-clipboard.

7

If you just need this behavior for mouse selections, you can use the built-in setting mouse-drag-copy-region:

(setq mouse-drag-copy-region t)

If non-nil, copy to kill-ring upon mouse adjustments of the region.

  • Most of the time, I use keyboard to select text. Is there a way to enable it for key board selection too? – ChillarAnand Oct 6 '15 at 13:54
  • @ChillarAnand I don't think there's a built-in option, no. You'll have to wait for someone more knowledgeable to answer. I'm not sure what the best way is to go about this. – nanny Oct 6 '15 at 14:11
  • This also works on non-X11 (e.g. Mac OS X) – mike May 3 '16 at 21:12
  • @ChillarAnand: on Mac OSX, this command works for both mouse and keyboards selections. Unbelievable but true – Doomsday Feb 16 '18 at 10:53

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.