Inverse selection


Hello all,

I have been using Sublime Text for a while and recently trying to move to Atom.

In ST, I can arbitrarily select a section of code in a long file, and do an inverse selection then use the fold function to focus on that particular section.

I can’t do this in Atom. I searched the forum and googled but found no answer about inverse selection.

I wonder is there any other simple way in Atom to do the same thing?



I looked for such a package and didn’t find it, so I decided to figure out the code myself. If you put this in your and assign a key to it (I found that ctrl-i was completely unused), it’ll do what you want. Oddly, there doesn’t seem to be a good way to get the coordinates of the end of the buffer without selecting everything. Fortunately, computers run fast enough that nothing actually shows up when you select everything and then immediately clear it.

# Invert selection
atom.commands.add 'atom-workspace', 'user:invert-selection', ->
  return unless editor = atom.workspace.getActiveTextEditor()

  selections = editor.getSelections()
  i = 0
  stops = {}
  for selection in selections
    stops[i] = selection.getScreenRange().start
    stops[i] = selection.getScreenRange().end

  end = editor.getLastSelection().getScreenRange().end

  editor.addSelectionForBufferRange([[0,0], stops[0]])

  l = 1
  while l < i - 1
    editor.addSelectionForBufferRange([stops[l], stops[l + 1]])
    l = l + 2

  editor.addSelectionForBufferRange([stops[l], end])

And in keymap.cson:

  'ctrl-i': 'user:invert-selection'


Thank you very much. It’s a smart hack :slight_smile:



I believe it is documented somewhere (but not sure where) that you can use Infinity for a coordinate if you want to specify “until the end of the line” or “until the end of the buffer”. You can see it used in a lot of Atom’s specs:


Well that simplifies things. I’d expect it to be documented on the Point page, and then still go unnoticed except by nerds like me who read the Point page.