Expand double click selection to the left


I have a variable in a PHP file:


Action: double click on variable
Result: foo is selected

Which is good. I don’t want $foo at this time, so I keep $ in the list of Non Word Characters.

Action: press shift+left
Result: fo is selected, because the selection is contracted from the right

Desired result: $foo is selected, by expanding the selection from the left

Is this possible?


It isn’t possible currently, but that doesn’t mean it can’t be added. If one could make the assumption that the idea is to always expand a selection rather than shrink it, then perhaps the Shift+Direction keys can be modified to behave this way … or some other key combination can be configured this way.


No idea yet on how to put this together, but what I found so far.

If you press cmd + . you can see the used commands for the used keys. Pressing shift+left resolves to core:select-left, so that’s already what it should do. If I select the word with the mouse, going from right to left, it will expand to the left. When double clicking a word though, Atom places the cursor at the end, causing it to move left (unselecting from the right). I missed this at first, but the cursor position is listed at the bottom, as well as the selection count. E.g. 12,22 (4) = row 12, column 22, 4 characters selected.

That information should be available and usable somehow. If there’s a way to bind shift+left to a custom command saying something like core:move-left(selection-count) + core:select-left, that’d be it. However… that would mean that you can never contract a selection anymore (shift+right+right+right+left would also jump back to the beginning, which isn’t what you want).

So the easiest way would be to leave the selection command alone, and change the cursor location after the double click. This probably maps to editor:select-word like ctrl + W. Not sure how though. I’m used to this behaviour after a decade of TextMate, so it’s hard to let go =)

Edit: getting closer: https://github.com/atom/atom/blob/v0.152.0/src/selection.coffee

  # Public: Modifies the selection to encompass the current word.
  # Returns a {Range}.
  selectWord: ->
    options = {}
    options.wordRegex = /[\t ]*/ if @cursor.isSurroundedByWhitespace()
    if @cursor.isBetweenWordAndNonWord()
      options.includeNonWordCharacters = false

    @wordwise = true
    @initialScreenRange = @getScreenRange()


I’m new to Atom, but here’s a +1 for a solution to this. I think shift+arrow should always expand the selection in the direction given, not simply modify the endpoint of the current selection. Reading up on Hacking Atom, and watching this thread.


That would be a great package. Good for a first package to cut your teeth on. I would give it a try, but it would take getting used to. I’m not sure how hard it would be to replace the old behavior.

Edit: I meant how hard it would be for me as a user to do the old behavior, not how hard it would be to write the new package.


Yes. The first movement could decide whether to move the beginning or the end of the selection, and once that’s decided, one can also shrink it. So if you select something, then shift-right, then you can now move the rear end of the selection. If you select something, then shift-left, then you can now move the head end of the selection.