Sublime Forum

How to clear selection without losing caret?

#1

I’m wondering how to tell sublime to “drop” the current selection and leave the caret where it is. (Doing this with a key binding.) Right now, this is my function “ClearSelectionsCommand”:

class  ClearSelectionsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.sel().clear()

It works, but a little too well, since not only does the selection disappear, but also the caret! (I deduce from this that the caret is a special instance of a selection?)

How can I abort the current selection(s) while leaving the caret(s) where they are?

0 Likes

#2

You are right, the caret is a selection of length 0.

Here’s a quick and dirty solution which restores the carets after removing selections.

import sublime_plugin


class ClearSelectionsCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        """Clear all selelections and keep carets."""
        sel = self.view.sel()
        if sel:
            carets = [s.b for s in sel]
            sel.clear()
            for caret in carets:
                sel.add(caret)

1 Like

Execute key binding only if status bar is visible?
#3

Thanks, it worked! :kissing_heart:

0 Likes