Sublime Forum

Open file from selected text

#1

Is there a way to open a file (if file) from selected text… For example if I highlight a class name, is there a way to open that file in a new tab?

0 Likes

#2

“goto anything” pre-populated with the selected text would be awesome. Ditto!

0 Likes

#3

If it’s for Python or Magento (PHP) code I’ve something working.

0 Likes

#4

Here’s a plug-in I wrote a little while back that does that.

It’s intended to work like Ctrl+Enter in Borland Delphi: open the thing under the cursor as a file.

It will try in the current path first, and if not found, it will look in a list of other paths, and will try each of a list of extensions.

For example, I have a library called “GenUtils.py” that I keep in a \projects folder, on different drives between my home and work computers.

I can click on “import GenUtils” in a piece of code, and this plugin will search for and open the first file matching “GenUtils.py” or “GenUtils.txt” in any folder named in the list.

Copy the code below into your plugin folder as OpenFile.py

   ...\Sublime Text\Data\Packages\User\OpenFile.py

Add a keystroke combination to your user keymap file

  <binding key="ctrl+enter" command="openFile" />

(Ctrl+Enter is initially assigned to “Add Line.sublime-macro”)

Make sure to edit the “paths” and “exts” lists below to match your preferences.

If you have questions or run into something that doesn’t work, please let me know.

Todd

# OpenFile.py
# open filename at cursor in SublimeText
# Todd Fiske (toddfiske at gmail) 

# 2010-12-23 12:17:48  first version
# 2011-01-14 13:24:33  added MakeName generator to handle common extensions
# 2011-04-04 12:59:19  updated for Sublime Text forum

import sublime, sublimeplugin
import os

paths = ".", "h:\\projects", "c:\\projects"]
exts  = ".py", ".txt"]

def MakeName(paths=], name="stub", exts=]):
    for p in paths:
        for e in exts:
            thisName = os.path.join(p, name + e)
            yield thisName

class OpenFileCommand(sublimeplugin.TextCommand):
    def run(self, view, args):
        print "OpenFile: cwd = %s" % os.getcwd()

        for region in view.sel():
            if region.empty():
                # no selection, get word at cursor
                savedWordSeps = view.options().get("wordSeparators")
                view.options().set("wordSeparators", "")
                word = view.word(region)
                lineContents = view.substr(word)
                view.options().set("wordSeparators", savedWordSeps)

            else:
                #- else get selection
                lineContents = view.substr(region)

            #- remove any leading comment or space characters
            if len(lineContents) > 0:
                while (lineContents[0] in "# "):
                    lineContents = lineContents[1:]

            #- try to open unmodified input first
            fileName = lineContents
            print "  trying %s]" % fileName
            found = os.path.exists(fileName)

            if not found:
                for fileName in MakeName(paths, fileName, exts):
                    print "  trying %s]" % fileName
                    found = os.path.exists(fileName)
                    if found:
                        break

            if found:
                print "  opening %s" % fileName
                sublime.activeWindow().openFile(fileName)
            else:
                print "  no matching file was found"

###
0 Likes