Prevent copying empty selections in Atom

I really can’t do with an editor that clears my clipboard when I accidently copy an empty line.

This is an easy mistake to make:

  • cut something with Cmd+X
  • move the cursor to an empty line where I want to paste
  • accidently hit Cmd+C instead of Cmd+V
  • Boom! I have now lost my clipboard contents

Sublime Text had an option to disable copying empty selections but Atom doesn’t. Fortunately, it’s super easy to hack Atom and someone has figured out how to prevent this. I’m pasting it here so I can easily find it.

This is the original post on the Atom forum and here’s the snippet to paste in your Atom init script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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()