Home Download Buy Blog Forum Support

How can I expand_selection to a sentence?

How can I expand_selection to a sentence?

Postby Kicweed on Tue Jul 10, 2012 8:02 am

I'd like to be able to select the sentence under my cursor. In essence I'd like something like vim's vis or vas commands: Select all the text between two fullstops, including the ending fullstop.

I've looked in various .sublime-commands but I can't find the right argument to pass to the expand_selection command. All I see is for brackets, tags, paragraphs etc.

Unless I'm mistaken, Vintage mode does not support selecting sentences either.
Kicweed
 
Posts: 22
Joined: Mon Jan 09, 2012 9:48 am

Re: How can I expand_selection to a sentence?

Postby Kicweed on Tue Dec 04, 2012 3:45 pm

Still no update on this one, eh?

I guess it could be implemented on top of a plugin like Bracket Highlighter, but instead of matching against brackets, it would match against fullstops.
Kicweed
 
Posts: 22
Joined: Mon Jan 09, 2012 9:48 am

Re: How can I expand_selection to a sentence?

Postby agibsonsw on Tue Dec 04, 2012 7:56 pm

It might be more fruitful to investigate the move command with the argument "extend":True:

Code: Select all
self.view.run_command("move", {"by": "stops", "extend":False, "forward":False, "word_begin":True, "punct_begin":True, "separators": ""})

There may be a clue in this key-binding:

Code: Select all
   { "keys": ["]"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
      [
         { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
         { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
         { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
      ]
   },

If you decide to create or extend a .py plug-in then you might side-step 'run_command' and manipulate sel() directly using the ST-API.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: How can I expand_selection to a sentence?

Postby russmcb on Sat Mar 23, 2013 1:30 am

Any progress on this front? I'm pretty surprised to come from Vim and find that I can't do v i s or v i p, for example. There is no sentence selection or paragraph selection from command mode. ???
russmcb
 
Posts: 3
Joined: Tue Jan 01, 2013 8:26 am

Re: How can I expand_selection to a sentence?

Postby skuroda on Sat Mar 23, 2013 3:11 am

You would have to write a small plugin to select a sentence (probably using view.find() to get the end point and using the cursor as a start point). There is a built in command for paragraph selection. You can create a keybinding with the following command "expand_selection_to_paragraph"
skuroda
 
Posts: 677
Joined: Fri Oct 05, 2012 8:11 am

Re: How can I expand_selection to a sentence?

Postby robertcollier4 on Sat Mar 23, 2013 8:09 am

Easily done via Python API, add a keybinding for "expand_selection_to_sentence" and add a .py file to your User folder with following contents:

Code: Select all
import sublime, sublime_plugin, string

class ExpandSelectionToSentenceCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      view = self.view
      oldSelRegions = list(view.sel())
      view.sel().clear()
      for thisregion in oldSelRegions:
         thisRegionBegin = thisregion.begin() - 1
         while ((view.substr(thisRegionBegin) not in ".") and (thisRegionBegin >= 0)):
            thisRegionBegin -= 1

         thisRegionBegin += 1
         while((view.substr(thisRegionBegin) in string.whitespace) and (thisRegionBegin < view.size())):
            thisRegionBegin += 1

         thisRegionEnd = thisregion.end()
         while((view.substr(thisRegionEnd) not in ".") and (thisRegionEnd < view.size())):
            thisRegionEnd += 1

         if(thisRegionBegin != thisRegionEnd):
            view.sel().add(sublime.Region(thisRegionBegin, thisRegionEnd+1))
         else:
            view.sel().add(sublime.Region(thisRegionBegin, thisRegionBegin))
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

Re: How can I expand_selection to a sentence?

Postby Kicweed on Tue Jun 25, 2013 2:31 pm

robertcollier4 wrote:Easily done via Python API, add a keybinding for "expand_selection_to_sentence" and add a .py file to your User folder with following contents:

Code: Select all
...



That worked flawlessly. Thank you very much.
Kicweed
 
Posts: 22
Joined: Mon Jan 09, 2012 9:48 am


Return to General Discussion

Who is online

Users browsing this forum: BestGreek, Google [Bot] and 16 guests