Skip to content

Instantly share code, notes, and snippets.

@robertcollier4
Last active December 15, 2015 14:39
Show Gist options
  • Save robertcollier4/5276374 to your computer and use it in GitHub Desktop.
Save robertcollier4/5276374 to your computer and use it in GitHub Desktop.
expand_selection_to_delims for SublimeText, use with { "keys": ["ctrl+shift+d"], "command": "expand_selection_to_delims"},
class ExpandSelectionToDelimsCommand(sublime_plugin.TextCommand):
def run(self, edit):
begindelims = ["\"", "\'", "(", "<", "[", "{"]
enddelims = ["\"", "\'", ")", ">", "]", "}"]
view = self.view
oldSelRegions = list(view.sel())
for thisregion in oldSelRegions:
thisRegionBegin = thisregion.begin() - 1
thisRegionEnd = thisregion.end()
if( (thisregion.begin() != thisRegionEnd) and (view.substr(thisRegionBegin) in begindelims) ):
thisRegionBegin -= 1
while ((view.substr(thisRegionBegin) not in begindelims) and (thisRegionBegin >= 0)):
thisRegionBegin -= 1
thisRegionBegin += 1
if( (thisregion.begin() != thisRegionEnd) and (view.substr(thisRegionEnd) in enddelims) ):
thisRegionEnd += 1
while((view.substr(thisRegionEnd) not in enddelims) and (thisRegionEnd < view.size())):
thisRegionEnd += 1
view.sel().add(sublime.Region(thisRegionBegin, thisRegionEnd))
@certainlyakey
Copy link

This does not work ok, after the first expansion it expands to wrong (non mirrored) delimiters.

@robertcollier4
Copy link
Author

It turns out that the original Gist is unncessary since Sublime has a built-in command for "expand_selection" which takes various arguments as shown in the following:


​Add to .sublime-menu

​​{
"caption": "Selection Expand",
"id": "selection",
"children":
[
{ "command": "expand_selection_to_whitespace", "caption": "Expand Selection to Whitespace" },
{ "command": "expand_selection", "args": {"to": "line"}, "caption": "Expand Selection to Line" },
{ "command": "find_under_expand", "caption": "Expand Selection to Word" },
{ "command": "expand_selection_to_paragraph", "caption": "Expand Selection to Paragraph" },
{ "command": "expand_selection", "args": {"to": "scope"}, "caption": "Expand Selection to Scope" },
{ "command": "expand_selection", "args": {"to": "brackets"}, "caption": "Expand Selection to Brackets" },
{ "command": "expand_selection", "args": {"to": "indentation"}, "caption": "Expand Selection to Indentation" },
{ "command": "expand_selection", "args": {"to": "tag"}, "caption": "Expand Selection to Tag" },
{ "command": "invert_selection" },
]
},


​Add to .sublime-​keymap

​​​{ "keys": ["ctrl+shift+q"], "command": "expand_selection_to_quotes"},
{ "keys": ["ctrl+shift+b"], "command": "expand_selection", "args": {"to": "brackets"} },
{ "keys": ["ctrl+space"], "command": "expand_selection_to_delims" },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment