Often in my personal readme.txt files / log files, I have some references to other .txt files. It can be relative path (..\notes\blah.txt
) or absolute path (like on the screenshot below).
Is it possible, for a .txt file, to have this behaviour in Sublime: double-click on a filename to open it in a new tab?
Note: Clickable URLs plugin is not really usable for me, because it parses the whole file, highlights the URLs (thanks to a nice regex), and then listens to CTRL+ALT+ENTER: if more than ~ 500 highlighted items, Sublime Text becomes highly unresponsive.
Here is a working solution.
First put this file lllaunch.py
in your user packages (e.g. C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\
):
import sublime, sublime_plugin
import subprocess
import webbrowser
import re
BROWSER = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
EDITOR = 'C:\Program Files\Sublime Text 2\sublime_text.exe'
class LllaunchCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
s = self.view.substr(self.view.line(region))
i = region.begin() - self.view.line(region).begin()
start = 0
end = -1
for j, c in enumerate(s):
if c == ' ':
if j < i:
start = j
else:
end = j
break
word = s[start:end].strip() if end != -1 else s[start:].strip()
isurl = bool(re.match("\\bhttps?://[-A-Za-z0-9+&@#/%?=~_()|!:,.;']*[-A-Za-z0-9+&@#/%=~_(|]", word))
if isurl:
webbrowser.register('mybrowser', None, webbrowser.GenericBrowser(BROWSER))
webbrowser.get('mybrowser').open(word)
else:
s = '"' + s.split('"')[s[:i].count('"')] + '"'
command = '"%s" %s' % (EDITOR, s)
subprocess.Popen(command)
Then add the following line to C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap
:
{ "keys": ["ctrl+alt+enter"], "command": "lllaunch" }
Restart Sublime Text.
Now:
"C:\test\readme.txt"
) will edit the file in your favorite editor.