I have added the underscore character to the list of word delimiters and implemented the following code to be able to press ctrl+w several times to expand to the next word part:
class IntelligentExpandSelection(sublime_plugin.TextCommand):
def run(self, edit, args=None):
after = self.view.substr(self.view.sel()[0].end())
if after == "_":
self.view.run_command("move", {"by": "characters", "forward": True, "extend": True})
self.view.run_command("expand_selection", {"to": "word"})
This works well for expanding forward. However, when I include code to expand backward too, by setting “forward” to False like so
before = self.view.substr(self.view.sel()[0].begin() - 1)
if before == "_":
self.view.run_command("move", {"by": "characters", "forward": False, "extend": True})
the selection shrinks by one character at the rightmost part of the selection, not by extending to the left, as I had hoped. How can I extend the selection by one character to the left?