Sublime Forum

How to edit MoveText plugin so it moves by word instead of by character

#1

I’d like to modify the MoveText plugin so it moves the selection left or right by one word at a time instead of one character at a time. How might I do it? Here’s the relevant function:

def move_text_horiz(self, edit, direction, selections=None):
    selections = selections or list(self.view.sel())
    if direction > 1:
        selections.reverse()
    for region in selections:
        if region.empty():
            continue

        orig_region = region
        sel_region = Region(region.begin() + direction, region.end() + direction)

        if sel_region.a < 0 or sel_region.b > self.view.size():
            continue

        if direction < 0:
            dest_region = Region(region.begin() + direction, region.end())
            move_text = self.view.substr(region) + self.view.substr(Region(region.begin() + direction, region.begin()))
        else:
            dest_region = Region(region.begin(), region.end() + direction)
            move_text = self.view.substr(Region(region.end(), region.end() + direction)) + self.view.substr(region)

        # Remove selection from RegionSet
        self.view.sel().subtract(orig_region)
        # Replace the selection with transformed text
        self.view.replace(edit, dest_region, move_text)
        # Add the new selection
        self.view.sel().add(sel_region)
1 Like