Sublime Forum

Expand/Contract selections

#1

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

[code]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]

{ "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} },

0 Likes

#2

Could you please update this very useful plugin for Sublime Text 3? It produces an error in Sublime Text 3 console:

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
0 Likes

#3

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

0 Likes

#4

[quote=“bizoo”]Remove the parenthesis after xpos, in ST3 it’s not a method anymore.
You can find the ST3 version here:
github.com/bizoo/Sublime-Select … ections.py[/quote]

Thanks a lot, it works again.

0 Likes