FilePathToClipboard.py
import sublime_plugin
import subprocess
def getClipboardData():
p = subprocess.Popen('pbpaste'], stdout=subprocess.PIPE)
data = p.stdout.read()
return data
def setClipboardData(data):
p = subprocess.Popen('pbcopy'], stdin=subprocess.PIPE)
p.stdin.write(data)
p.stdin.close()
class FilePathToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
setClipboardData("/* " + self.view.file_name().replace("/Users/your_username", "~") + " */")
Keybinding I setup:
{ "keys": "super+shift+c"], "command": "file_path_to_clipboard" }
OpenPaths.py
import sublime_plugin
import os
from os import path, access, R_OK # W_OK for write permission.
import re
class OpenWordFilePathCommand(sublime_plugin.TextCommand):
def run(self, edit):
searchPath = self.view.file_name().strip(path.basename(self.view.file_name()))
searchPath = searchPath.strip("controllers/")
myFile = None
for region in self.view.sel():
if not region.empty():
myFile = self.view.substr(region).strip(" \n\t\r")
myFile += ".php"
for subdir, dirs, files in os.walk("/" + searchPath + "/"):
for file in files:
if file == myFile:
myFilePath = os.path.join(subdir, file)
print myFilePath
if path.exists(myFilePath) and path.isfile(myFilePath) and access(myFilePath, R_OK):
self.view.window().open_file(myFilePath)
else:
print "Either file is missing or is not readable"
class OpenLineFilePathCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
str1 = self.view.substr(region).strip(" \n\t\r")
else:
line = self.view.line(region)
str1 = self.view.substr(line).strip(" \n\t\r")
r = re.compile('/\*(.*?)\*/')
m = r.search(str1)
if m:
filePath = m.group(1).strip(" \n\t\r")
filePath = path.expanduser(filePath)
if path.exists(filePath) and path.isfile(filePath) and access(filePath, R_OK):
self.view.window().open_file(filePath)
else:
print "Either file is missing or is not readable"
else:
print "This is not a file path"
The OpenLineFilePathCommand will open a complete path that the FilePathToClipboardCommand gives you.
The OpenWordFilePathCommand basically moves back a directory to look in my views directory for the one word file I’ve selected. I did this because it works for most cases in my workflow and removed the need to have a commented file path in my code most of the time.
Here is the mousemap file
{
"button": "button1", "count": 1,
"modifiers": "alt", "ctrl"],
"press_command": "drag_select",
"press_args": {"by": "words"},
"command": "open_word_file_path"
},
{
"button": "button1", "count": 1,
"modifiers": "alt", "ctrl", "super"],
"press_command": "drag_select",
"press_args": {"by": "lines"},
"command": "open_line_file_path"
}
]
All of this stuff may only work for Mac. I don’t use windows so I haven’t checked to see if it works.