Sublime Forum

Relative goto line number

#1

Like already write in a post, I need sometimes a relative goto line number from the current position:
+2 -> Current line + 2
-3 -> Current line - 3

ST is so unbelievable :astonished: customizable that you could even change the goto line number command, you’re great Jon !!!

So this is my version of goto_line.py that I’ve put in my User directory.
I hope you think about use it for the official release.
The only drawback is that the line parameter of the GotoLineCommand is now a text, not an integer.

[code]import sublime, sublime_plugin

class PromptGotoLineCommand(sublime_plugin.WindowCommand):

def run(self):
    self.window.show_input_panel("Goto Line:", "", self.on_done, None, None)
    pass

def on_done(self, text):
    try:
        line = int(text)
        if self.window.active_view():
            self.window.active_view().run_command("goto_line", {"line": text} )
    except ValueError:
        pass

class GotoLineCommand(sublime_plugin.TextCommand):

def run(self, edit, line):
    # line number preceding by + or - are relative
    is_relative = line[0] in ('+', '-')
    
    # Convert from 1 based to a 0 based line number
    line = int(line) - 1

    if is_relative:            
        lines, _ = self.view.rowcol(self.view.sel()[0].begin())
        line = lines + line + 1

    pt = self.view.text_point(line, 0)

    self.view.sel().clear()
    self.view.sel().add(sublime.Region(pt))

    self.view.show(pt)[/code]
0 Likes