Sublime Forum

Click Keybindings

#1

I’ve made a plugin that allows me to copy the current file’s path as a commented string into the clipboard. I then can paste this into a relevant spot in my code for easy file reference/access. I then created a plugin that is binded to super+shift+l that basically takes the line the cursor is currently on and checks to see if it is a path and if the path exists and then opens that file. In addition to placing the cursor at the line and then using the keybinding, I’d like to just be able to double click the line and it would run the same plugin, like how double clicking works when you search for something in a project and it shows you all the results.

Is this possible?

0 Likes

Double click not working on macOS
#2

Yes, create a file named “Default.sublime-mousemap” in your plugin directory looking something like this:

    {
        "button": "button1", "count": 2,
        "press_command": "drag_select",
        "press_args": {"by": "words"},
        "command": "your_command_here"
    }
]

“your_command_here” will now run when double clicking.

0 Likes

#3

Awesome! Thank You. For some reason the count 2 wasn’t working, but count 3 did as well as what I ended up going with which is 1 click with a ctrl+alt modifier.

Also, I had to use "press_args": {"by": "lines"} instead of "press_args": {"by": "words"}

Here is the final:

	
    {
        "button": "button1", "count": 1,
        "modifiers": "alt", "ctrl"],
        "press_command": "drag_select",
        "press_args": {"by": "lines"},
        "command": "your_command_here"
    }
]
0 Likes

#4

Hi Patrick,
could you please share such a plug-in?
I’d appreciate it so much (I’m actually trying to do something similar…).

Many thanks in advance,

bblue

0 Likes

#5

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.

0 Likes

#6

Thank you so much!

0 Likes