Sublime Forum

Cmd-L to select lines, but what about deselecting?

#1

I know that I can keep selecting subsequent lines via Command-L, but is there a way to undo/deselect one line at a time if I previously selected it? Similar to how if you do multiple words with Command-D, you can press Command-U to undo the last selected word. If I do Command-U for multiple Command-L’s, then it undos all the selections except the first.

Thanks!

0 Likes

#2

[code]#coding: utf8
#################################### IMPORTS ###################################

Sublime Libs

import sublime_plugin

################################################################################

class PopSelections(sublime_plugin.TextCommand):
“”"
{
“keys”: “ctrl+alt+up”], “args”: {“forward”: false },
“command”: “pop_selections”,
“context”:
{
“key”: “selection_empty”,
“operator”: “equal”, “operand”: false, “match_all”: false
}
]
},
{
“keys”: “ctrl+alt+down”], “args”: {“forward”: true },
“command”: “pop_selections”,
“context”:
{
“key”: “selection_empty”,
“operator”: “equal”, “operand”: false, “match_all”: false
}
]
}
“”"
def is_enabled(self, forward):
return len(self.view.sel()) > 1

def run(self, edit, forward=True):
    view = self.view
    view.sel().subtract(view.sel()[0 if forward else -1])

################################################################################
[/code]
If you are like me, you typically use the select_lines command only with empty selections. I just bound over the bindings for that with something that pops selections. You might wanna binding for when the selections are empty.

You might also like this command in combination with split by lines:

[code]class ModuloSelections(sublime_plugin.TextCommand):
def run(self, edit, m=2, n=1):
view = self.view

    for i, sel in enumerate(list(view.sel())):
        if not i % int(m) == int(n) - 1:
            view.sel().subtract(sel)

[/code]
Say you want the 1st of every 2 selections you’d run a binding like so:

{ "args": { "m": "2", "n": "1" }, "command": "modulo_selections", "context": ], "keys": "ctrl+shift+5" ] },
Change n to 2 for a binding for the 2nd of every 2 selections. You get the gist …

0 Likes