Sublime Forum

Expand selection to left in OS X

#1

I’m sorry if this has been covered already; maybe I’m searching with the wrong keywords but I can’t find much on this.

Let’s say I have this piece of PHP code:

$foo'bar'] = 1; $foo'baz'] = 2; $this->foo = 3;

Sometimes I also want to select “$foo” instead of just “foo” by double clicking the word. This can be changed by adding “$” to the “word_separators” setting. However, I often want to replace all the “foo” occurrences in the text, which can easily be done using CMD + D on the first “foo”, and then a few more CMD +D’s.

So I don’t include “$” in the word_separators, but I still want to be able to select the “$” sign after selecting the word “foo”.

On the Mac, when I select “foo” in “$foo” and press SHIFT + arrow left, “$foo” is selected, adding “$” to the selection of “foo”. In ST2 on the Mac though, when I press SHIFT + arrow left, the selection doesn’t expand to the left, but moves back from the right, resulting in selecting “fo”.

I’ve tried all sorts of combinations of alt, control, etc. when trying to expand the selection to the left, but it doesn’t seem to work. Is there some kind of setting or plugin available to use the default OS X selection behavior?

0 Likes

#2

Bump. I’ve looked every once in a while if this has changed in SublimeText, but haven’t found anything yet.

Does anyone know if there is something in ST3 that would make this possible?

I’d really like to use ST3 as my default editor, but this simple thing just completely breaks my workflow.

0 Likes

#3

I don’t have an OS X machine to test on, but the expansion of selections is based on the cursor position. For example, if it deselecting an “o” in the presented example, the cursor is probably after the last “o”. To highlight the “$” I would assume the cursor is before “f”. You could probably write a plugin to reverse the cursor position on some condition, though it may not be quite as reliable as you would like. In windows, if I select word (via double clicking or ctrl+d) the cursor is placed at the end of the word. Perhaps it’s different in OS X.

0 Likes

#4

I had a go with this, as exactly skuroda described. This plugin swaps the cursor position of all the current selected regions. So it can “grow” to other direction after swapping. Here is the code that you can add as plugin. I have tried it a bit, it seems to work. I am not entirely sure that it will be absolutely problem free though. Do let me know.

import sublime, sublime_plugin

class ReverseSelectionCursorsCommand(sublime_plugin.TextCommand):
    """ reverse the .a and .b of all selections, this swaps the cursor between
    the front and the end of selected regions, so that the selected region can
    grow to the other direction. """
    def run(self, edit):
        aa, bb = ], ]
        for r in self.view.sel():
            aa.append(r.a)
            bb.append(r.b)
        self.view.sel().clear()
        for a, b in zip(aa,bb):
            self.view.sel().add(sublime.Region(b,a))

Then you can add a key binding to trigger the command easily. eg.

{ "keys": "ctrl+alt+r"], "command": "reverse_selection_cursors" },

0 Likes