Skip to content

Instantly share code, notes, and snippets.

@Suor
Created November 7, 2011 08:41
Show Gist options
  • Save Suor/1344471 to your computer and use it in GitHub Desktop.
Save Suor/1344471 to your computer and use it in GitHub Desktop.
open_file_at_cursor sublime command
cd ~/.config/sublime-text-2/Packages/User/
curl -O https://raw.github.com/gist/1344471/open_file_at_cursor.py

Open keyboard bindings file, and add a line to it

[
    ...
    { "keys": ["alt+o"], "command": "open_file_at_cursor" } // this one
]
import sublime, sublime_plugin
import os.path, string
import re
VALID_FILENAME_CHARS = "-_.() %s%s%s" % (string.ascii_letters, string.digits, "/\\")
filename_re = re.compile(r'[\w/\.-]+')
class OpenFilenameUnderCursor(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
# Find anything looking like file in whole line at cursor
whole_line = self.view.substr(self.view.line(region))
row, col = self.view.rowcol(region.begin())
while col >= len(whole_line) or whole_line[col] in VALID_FILENAME_CHARS:
col -= 1
m = filename_re.search(whole_line, col)
if m:
filename = m.group()
print "Opening file '%s'" % (filename)
self.view.window().open_file(filename)
else:
print "No filename discovered"
@pavelloz
Copy link

pavelloz commented May 21, 2021

It throws

Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 308, in reload_plugin
    m = importlib.import_module(modulename)
  File "./python3.3/importlib/__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1022, in load_module
  File "<frozen importlib._bootstrap>", line 1003, in load_module
  File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 853, in _load_module
  File "<frozen importlib._bootstrap>", line 980, in get_code
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/Users/pavel/Library/Application Support/Sublime Text 3/Packages/open_file_at_cursor.py", line 20
    print "Opening file '%s'" % (filename)
                            ^
SyntaxError: invalid syntax

Ive read in ST release notes that it uses python 3.8 now, but stack trace is using 3.3.
But! It seems that it installed 3.8 as well:

Packages|⇒ ll /Applications/Sublime\ Text.app/Contents/MacOS/Lib
96B May 20 10:57 python3
2.5M May 20 10:57 python3.3.zip
2.9M May 20 10:57 python3.8.zip
128B May 20 10:57 python33
128B May 20 10:57 python38

@Suor
Copy link
Author

Suor commented May 21, 2021

Yeah, this is python 2 style prints. Maybe if you change them to print() style it will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment