[Resolved] Avoid copying when selection is empty


Is there a way to disable copy (ctrl-c) when selection is empty ?
In Sublime Text, I can do that by setting the copy_with_empty_selection option to false.
I’d love this because I often hit ctrl-c instead of ctrl-v and it override my current clipboard.


I’m not aware of a package or config too change that and I don’t think atom core will change it because copy on empty, copy all the current line. But maybe this package could help you https://atom.io/packages/clipboard-history


You could probably create your own custom copy command in the init.coffee, but I don’t have a great idea on how to get started with that. Someone else want to take a stab at it? @olmokramer perhaps?


How did you know I would? :wink:

Here you go:

# prevent core:copy if and only if there's one selection in
# the active editor (mini or not) and its length equals 0
atom.commands.add 'atom-text-editor', 'core:copy', (e) ->
  editor = e.currentTarget.getModel()

  # do nothing if there's more than 1 selection
  return if editor.getSelectedBufferRanges().length > 1

  # get the starting and ending points of the selection
  {start, end} = editor.getSelectedBufferRange()

  # stop the command from immediate propagation (i.e.
  # executing the same command on the same element or
  # an element higher up the DOM tree). This works
  # because atom executes commands in the reverse order
  # they were registered with atom.commands.add, and this
  # one's added after the core commands are already
  # registered.
  if start.column is end.column and start.row is end.row


Very nice, it works just fine and I learned something :+1:
The atom community is one the great things that make me love this editor.

Thank you :slight_smile:


Kudos! I found this very useful (and followed the same pattern for avoiding cut when selection is empty)


Another solution is now available. The copy-selection command was added which will only copy to clipboard if text is selected. Binding cmd-c to this command in your keymap file solves the issue.


This is wonderfull, where do I have to put this snippet @olmokramer ?


It goes in the init.coffee.


Sorry for hijacking this thread.

I’m pretty much an Atom n00b but do have software development experience.

In some IDEs, I’ve a plugin that will select the current word upon copying when nothing was selected.

I think @olmokramer 's snippet would be a good start for writing an atom extension that does this, but since I never wrote one: how would I get started?

Basic ideas:

  • optionally replace current copy behaviour (can I do this on the method level or shortcut level? If latter: how can I find out which keyboard/mouse/menu shortcuts access the copy action)
  • if there is a selection, run the current behaviour
  • if there is none, select word (maybe make the selection method configurable), then run the current behaviour



There are a couple tutorials on how to build an Atom package in the Atom Flight Manual. You can find them in the Hacking Atom section.

@olmokramer’s example shows everything you need to answer the questions you have. The Flight Manual should have the basic information on Atom commands that fill in some of the blanks and then the Atom API documentation will have the methods to achieve the specific actions you’re mentioning.


I know this is an old post, but since this pops up pretty high on Google’s results, you can find the solution to this here: ‘copy’ keyboard shortcut should not copy empty space

Basically, add this to your keymap.cson:

    'cmd-c': 'editor:copy-selection'   # mac os
    'ctrl-c': 'editor:copy-selection'  # linux and windows