Home Download Buy Blog Forum Support

Relative goto line number

Relative goto line number

Postby bizoo on Mon Jan 17, 2011 8:23 am

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 :o 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: Select all
import sublime, sublime_plugin

class PromptGotoLineCommand(sublime_plugin.WindowCommand):

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

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

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)


Posts: 958
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 6 guests