[Resolved] Avoid copying when selection is empty


#1

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.


#2

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


#3

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?


#4

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
    e.stopImmediatePropagation()

#5

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:


#6

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


#7

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.


#8

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


#9

It goes in the init.coffee.


#10

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

–jeroen


#11

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.


#12

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:

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