Home Download Buy Blog Forum Support

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

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

Postby ryanlecompte on Tue Jan 24, 2012 2:09 am

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!
ryanlecompte
 
Posts: 20
Joined: Wed Dec 28, 2011 2:16 pm

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

Postby sublimator on Tue Jan 24, 2012 5:57 am

Code: Select all
#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])

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

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: Select all
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)

Say you want the 1st of every 2 selections you'd run a binding like so:

Code: Select all
  {
    "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 ...
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am


Return to General Discussion

Who is online

Users browsing this forum: apessino and 15 guests