Scorb
Scorb

Reputation: 1850

Expand selection to custom line

I am wondering if there is a out of the box way, or a plugin that can achieve the following behaviour in SublimeText3.

I would like to put the caret at a certain line. And then select all the text until another line number. The amount of lines should be variable.

For example put the caret on 10 and then expand selection to line 21 or line 104.

I hate having to hold down key or use the mouse for this action.

Upvotes: 1

Views: 108

Answers (1)

Enteleform
Enteleform

Reputation: 3823

I wrote a simple plugin that allows you to enter a line to select until via an input_panel:

Demo


Features:

  • works bidirectionally
  • respects the current selection
  • only executes if there is a single selection

Setup Info:

@ GitHub


Code:

import sublime, sublime_plugin

class SelectToLineCommand( sublime_plugin.TextCommand ):

    def run( self, edit ):

        window     = self.view.window()
        selections = self.view.sel()

        if len( selections ) != 1:
            return

        self.currentSelection = selections[0]

        if self.currentSelection.a > self.currentSelection.b:
            self.currentSelection = sublime.Region( self.currentSelection.b, self.currentSelection.a )

        window.show_input_panel( "Select To Line Number", "", self.get_LineNumber, None, None )

    def get_LineNumber( self, userInput ):

        lineToRow_Offset = 1
        row = int( userInput ) - lineToRow_Offset
        selectionEnd_Row = self.view.text_point( row, 0 )

        currentSelection = self.currentSelection

        if selectionEnd_Row >= currentSelection.b:
            selectionStart = currentSelection.a
            selectionEnd   = self.view.line( selectionEnd_Row ).b
        elif selectionEnd_Row < currentSelection.a:
            selectionStart = currentSelection.b
            selectionEnd   = self.view.line( selectionEnd_Row ).a

        newSelection = sublime.Region( selectionStart, selectionEnd )

        self.view.selection.clear()
        self.view.selection.add( newSelection )

Upvotes: 3

Related Questions