Sublime Forum

[Solved] How to open file at specified line number

#1

Hi,
I would like to open a file from the API and jump to a specified line number. I thought it should work using following code:

v = sublime.active_window().open_file(filename)
v.run_command("goto_line", {"line": linenumber} )

but it works only if the file was already open when the code is executed. If the file was not yet open, it is opened, but at line 1. And I could nod figure out how to jump to another line. Why does my code not work and how can it be fixed?

0 Likes

#2

window.open_file("{}:{}:{}".format(fname, row, col), sublime.ENCODED_POSITION)

The other way would be to wait until v.is_loading() becomes False and then run goto_line, probably in a sublime.set_timeout_async callback.

3 Likes

#3

Thank you @FichteFoll, that solved my problem. I used

window.open_file("{}:{}:{}".format(fname, row, col))

but without the sublime.ENCODED_POSITION parameter :slight_smile: But now it works. THX!

0 Likes