Skip to content

Instantly share code, notes, and snippets.

@Suor
Created November 7, 2011 08:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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"
@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