Sublime Forum

Open file at specific line number using "open_file" command

#1

I have added some entries in my Default.sublime-commands file to open files, e.g.

{ "caption": "File Open: File Name", 
  "command": "open_file",
  "args": {"file": "/path/to/file.ext"} },

They work fine but I’d like one of them to open at a specific line number and can’t find the right notation.

This does NOT open the file at line 123, instead it opens the file path: /path/to/file.ext:123.

{ "caption": "File Open: File Name", 
  "command": "open_file",
  "args": {"file": "/path/to/file.ext:123"} },

Any ideas? Or should I write a plugin to do it?

1 Like

#2

Try adding “flags” : 1 to the argument list. The API open_file need to have the flag ENCODED_POSITION to extract the line/column from the file name, maybe it is the same.

{ "caption": "File Open: File Name", 
  "command": "open_file",
  "args": {"file": "/path/to/file.ext:123", "flags":1} },
0 Likes

#3

Thanks but no that did not work. As before the file /path/to/file.ext:123 was loaded.

All of the following args have been tried and failed with the same result as above:

"args": {"file": "/path/to/file.ext:123"}
"args": {"file": "/path/to/file.ext:123:0"}
"args": {"file": "/path/to/file.ext:123", "flags": 1}
"args": {"file": "/path/to/file.ext:123:0", "flags": 1}
"args": {"file": "/path/to/file.ext:123", "flags": "1"}
"args": {"file": "/path/to/file.ext:123:0", "flags": "1"}
"args": {"file": "/path/to/file.ext:123", "flags": "sublime.ENCODED_POSITION"}
"args": {"file": "/path/to/file.ext:123:0", "flags": "sublime.ENCODED_POSITION"}
"args": {"file": "/path/to/file.ext:123", "flags": "ENCODED_POSITION"}
"args": {"file": "/path/to/file.ext:123:0", "flags": "ENCODED_POSITION"}

These long shots failed (as expected) with a Default.sublime-commands parse error:

"args": {"file": "/path/to/file.ext:123", "flags": sublime.ENCODED_POSITION}
"args": {"file": "/path/to/file.ext:123:0", "flags": sublime.ENCODED_POSITION}
0 Likes

#4

I stopped faffing around and wrote a plugin.

import sublime
import sublime_plugin

class OpenFileAtRowColCommand(sublime_plugin.WindowCommand):
    def run(self, file_path, row=0, col=0):
        file_path = "{}:{}:{}".format(file_path, row, col)
        self.window.open_file(file_path, sublime.ENCODED_POSITION)

Used with:

ST Command: open_file_at_row_col

e.g. For `Default.sublime-commands`

{ "caption": "File Open: File Name - Sub Section",
  "command": "open_file_at_row_col",
  "args": {"file_path": "/path/to/filename", "row": 123, "col": 0} },

e.g. For `Default (OS).sublime-keymap`

{ "keys": ["??+??"], "command": "open_file_at_row_col",
  "args": {"file_path": "/path/to/filename", "row": 123, "col": 10} },

All working file now.

1 Like

#5

ST rows and columns actually start at 1 not 0, so this is better:

import sublime
import sublime_plugin

class OpenFileAtRowColCommand(sublime_plugin.WindowCommand):
    def run(self, file_path, row=1, col=1):
        file_path = "{}:{}:{}".format(file_path, row, col)
        self.window.open_file(file_path, sublime.ENCODED_POSITION)

Also with the plugin the row and col values can be omitted, they both default to 1.

ST Command: open_file_at_row_col

e.g. For `Default.sublime-commands`

{ "caption": "File Open: File Name - Sub Section #1",
  "command": "open_file_at_row_col",
  "args": {"file_path": "/path/to/filename", "row": 123, "col": 1} },

{ "caption": "File Open: File Name - Sub Section #2",
  "command": "open_file_at_row_col",
  "args": {"file_path": "/path/to/filename", "row": 456} },

e.g. For `Default (OS).sublime-keymap`

{ "keys": ["ctrl+??"], "command": "open_file_at_row_col",
  "args": {"file_path": "/path/to/filename", "row": 123, "col": 10} },
0 Likes

#6

Open file at specific line number using “open_file” command

1 Like