Sublime Forum

[Solved] Goto position by byte offset

#1

I’m rather surprised that this feature does already exist (or maybe it does, I just can’t find documentation for it).

I’d like to move the cursor to a position in the file by byte offset. Anybody who has worked with text-based data files knows that this can be invaluable for debugging code (I’m knee deep in it now).

0 Likes

#2

Not built in but you can get that behavior with a plugin.

[code]import sublime
import sublime_plugin

class GotoPositionCommand(sublime_plugin.WindowCommand):
def run(self):
window = self.window
view = window.active_view()
self.original_cursors = ]
for cursor in view.sel():
self.original_cursors.append(cursor)
window.show_input_panel(“Byte Position”, “”, self.location_update, self.location_update, self.on_cancel)

def location_update(self, input_text):
    print(input_text)
    window = self.window
    view = window.active_view()
    try:
        location = int(input_text)
    except:
        return

    cursors = view.sel()
    cursors.clear()
    cursors.add(sublime.Region(location, location))
    view.show_at_center(location)

def on_cancel(self):
    window = self.window
    view = window.active_view()
    view.sel().clear()
    view.sel().add_all(self.original_cursors)
    view.show(view.sel())[/code]

Though with multibyte character this will likely have issues.

0 Likes

#3

Maybe you can try EasyMotion.
sublime.wbond.net/packages/EasyMotion

0 Likes

#4

@skuroda, please, may you correctly insert your plugin? Thanks.

0 Likes

#5

You might find that a plugin is not needed:

1 Like