Sublime Forum

Open File from code

#1

Hi all,

One of the things that I actually miss from Dreamweaver is the ability to open files when my cursor is on a file string, inside my code, pressing Ctrl+D will open the file in a new tab, allowing me to edit it.

I’d like to recreate this behaviour in ST, reducing the amount of clicking I have to do.

Any ideas on how I could do this? I’m thinking I’d have to set regions between two quotes, single and double, and check the structure of the text to see if it’s a “file”, then open it?

Yay, nay?

James

0 Likes

#2

I’d do something like this:

self.view.run_command("expand_selection", {"to":"scope"}) text = self.view.substr(self.view.sel()[0]) self.view.run_command("show_overlay", {"overlay":"goto","show_files": True, "text": text})
…except I can’t get self.view.run_command(“show_overlay”) to actually show an overlay. Maybe Jon can comment?
Also, this would only open the overlay with the filename pre-pasted in the box. If you want to open a file manually I think you’ll need to actually do some path-wrangling.

0 Likes

#3

The plugin below seems to work for me. If you have some text selected it will assume the selection is a file path. If no text is selected the it will grab the whole line and assume that is a file path. If the file doesn’t exist then nothing happens. Theoretically it should work with multiple selections but i have never tried it.

import sublime, sublime_plugin
import os.path

class OpenFilenameUnderCursor(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if region.empty():
            	line = self.view.line(region)
            	filepath = self.view.substr(line).strip()
            else:
            	filepath = self.view.substr(region)

            if os.path.isfile(filepath):
                print "Opening file '%s'" % (filepath)
                self.view.window().open_file(filepath)
            else:
                print "File does not exist: '%s'" % (filepath)

…using a key mapping something like this:

    { "keys": "alt+o"], "command": "open_filename_under_cursor" },
0 Likes

#4

[quote=“jbjornson”]The plugin below seems to work for me. If you have some text selected it will assume the selection is a file path. If no text is selected the it will grab the whole line and assume that is a file path. If the file doesn’t exist then nothing happens. Theoretically it should work with multiple selections but i have never tried it.

import sublime, sublime_plugin
import os.path

class OpenFilenameUnderCursor(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if region.empty():
            	line = self.view.line(region)
            	filepath = self.view.substr(line).strip()
            else:
            	filepath = self.view.substr(region)

            if os.path.isfile(filepath):
                print "Opening file '%s'" % (filepath)
                self.view.window().open_file(filepath)
            else:
                print "File does not exist: '%s'" % (filepath)

…using a key mapping something like this:

{ "keys": "alt+o"], "command": "open_filename_under_cursor" }, [/quote]

Awesome :smiley:

I’ll be making a few adjustments, then I’ll get it posted here :smile:

0 Likes