Home Download Buy Blog Forum Support

Expand/Contract selections

Expand/Contract selections

Postby bizoo on Tue Apr 23, 2013 9:58 am

A small plugin to expand/contract selections by one character to the left or/and to the right.

Code: Select all
import sublime
import sublime_plugin

class ExpandSelectionCustomCommand(sublime_plugin.TextCommand):
    def run(self, edit, expand, right=False, left=False):
        sels = []
        viewsize = self.view.size()
        inc = 1 if expand else -1
        for sel in self.view.sel():
            a = sel.a
            b = sel.b
            if not (sel.size() == 0 and not expand):
                if right:
                    if sel.b >= sel.a:
                        b = sel.b+inc
                    else:
                        a = sel.a+inc
                if left:
                    if sel.b > sel.a:
                        a = sel.a-inc
                    else:
                        b = sel.b-inc
            sels.append(sublime.Region(
                min(max(a, 0), viewsize),
                min(max(b, 0), viewsize), sel.xpos()))
        self.view.sel().clear()
        for sel in sels:
            self.view.sel().add(sel)


Code: Select all
    { "keys": ["shift+ctrl+super+right"], "command": "expand_selection_custom", "args": {"expand": true, "right": true} },
    { "keys": ["shift+ctrl+super+left"], "command": "expand_selection_custom", "args": {"expand": true, "left": true} },
    { "keys": ["alt+shift+ctrl+super+right"], "command": "expand_selection_custom", "args": {"expand": false, "left": true} },
    { "keys": ["alt+shift+ctrl+super+left"], "command": "expand_selection_custom", "args": {"expand": false, "right": true} },
    { "keys": ["shift+ctrl+super+up"], "command": "expand_selection_custom", "args": {"expand": true, "left": true, "right": true} },
    { "keys": ["alt+shift+ctrl+super+up"], "command": "expand_selection_custom", "args": {"expand": false, "left": true, "right": true} },
bizoo
 
Posts: 873
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Expand/Contract selections

Postby certainlyakey on Fri Jan 31, 2014 10:11 am

Could you please update this very useful plugin for Sublime Text 3? It produces an error in Sublime Text 3 console:
Code: Select all
File "E:\Dropbox\...\plugin_expand-contract-selections.py", line 26, in run
    min(max(b, 0), viewsize), sel.xpos()))
TypeError: 'float' object is not callable
certainlyakey
 
Posts: 10
Joined: Sat Jul 06, 2013 10:15 am

Re: Expand/Contract selections

Postby bizoo on Fri Jan 31, 2014 11:12 am

Remove the parenthesis after xpos, in ST3 it's not a method anymore.
You can find the ST3 version here:
https://github.com/bizoo/Sublime-Select ... ections.py
bizoo
 
Posts: 873
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Expand/Contract selections

Postby certainlyakey on Sat Feb 01, 2014 10:26 am

bizoo wrote:Remove the parenthesis after xpos, in ST3 it's not a method anymore.
You can find the ST3 version here:
https://github.com/bizoo/Sublime-Select ... ections.py

Thanks a lot, it works again.
certainlyakey
 
Posts: 10
Joined: Sat Jul 06, 2013 10:15 am


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 6 guests