Sublime Text 3 Key Binding - Using Variables

I'm trying to do keybinding to open my favorite git app with the current Sublime Text 3 $ project_path folder, but somehow Sublime Text 3 is not converting the variables to the actual path.

Below you can find the current keyboard binding file.

[
    {
        "keys": ["super+ctrl+alt+g"],
        "command": "exec",
        "args":
        {
            "shell_cmd": "open -a Gitbox $project_path"
        }
    }
]

      

$ project_path is not converted to actual project path ... What am I doing wrong? Should I use "Build System" instead? I have looked at build systems, but the problem is that you will need to select the file area (e.g. * .rb) and I want this keyboard shortcut to be valid for all my projects / files.

In textmate2, the same shortcut was easily achieved by creating a new "command" in the Bundle editor and assigning a shortcut to it. Then the command will be as follows:

#!/usr/bin/ruby
exec "open -a Gitbox '#{ENV['TM_PROJECT_DIRECTORY']}'"

      

So, I am trying to achieve the same goal in Sublime Text 3, but something is wrong.

Thank!

+3


source to share


2 answers


Edit:

In the meantime, I created a generic plugin that gives you the ability to open any external application with a keystroke or using the command palette. The plugin can be found here: ExternalTools

You can easily install it using the Command Palette cmd+shift+p

In your case, you can go to Preferences / Key Bindings and add the following:

{ 
    "keys": ["super+ctrl+alt+g"], 
    "command": "external_tools_run", 
    "args": { "cmd": ["open", "-a", "Gitbox", "$project_path"] }
} 

      

I still don’t own a poppy, so there’s a chance that it is not working as expected. In that case, I would be glad if you can give me feedback (see Issues ).




Original answer:

I spent several hours looking for the same problem. In the end, I decided to create a small plugin with my own site owners.

import sublime, sublime_plugin

class RunAppCommand(sublime_plugin.WindowCommand):
    def run(self, app_args):
        app_args = self.fill_placeholder(app_args)
        self.window.run_command("exec", {"cmd": app_args } )        

def fill_placeholder(self, args):
    res = []
    proj_folder = self.get_project_folder()

    for arg in args:
        arg = arg.replace("$PROJECT_FOLDER", proj_folder)
        res.append(arg)

    return res

def get_project_folder(self,default=None):
    proj_folder = ""

    if self.window.project_file_name():
        proj = self.window.project_data()

    proj_folder_obj = self.get_first(proj["folders"])
    if proj_folder_obj:
        proj_folder = proj_folder_obj["path"]

    elif self.window.folders():
        proj_folder = self.get_first(self.window.folders())

    if not proj_folder:
        sublime.status_message("No project folder located")
        return default

    return proj_folder        

def get_first(self, iterable, default=None):
    if iterable:
        for item in iterable:
        return item
    return default

      

After you have saved the above code in Packages / User / RunApp.py, you can make your command simply by adding the following to your Default.sublime-keymap:

{ "keys": ["super+ctrl+alt+g"], "command": "run_app", "args": { "app_args": ["open", "-a", "Gitbox", "$PROJECT_FOLDER"]}}

      

This may not be the best solution, but it works for me.

+2


source


Also spent a few hours searching here - thanks to fboers' example I could finally create the plugin myself.

So my solution is: exec2 - expands all sublime variables as it should and then redirects it to exec .. p>

Save this as Packages/Exec2/Exec2.py

.



import sublime
import sublime_plugin

class Exec2Command(sublime_plugin.WindowCommand):
    def run(self, cmd):
        new_cmd = [sublime.expand_variables (value, self.window.extract_variables()) for value in cmd]
        self.window.run_command("exec", {"cmd": new_cmd } )     

      

Example for key binding:

{ "keys": ["ctrl+F7"], "command": "exec2", "args" : { "cmd": ["V:/v4_tools/v4Doccer.exe", "--mdi", "${file}"]}  },

      

0


source







All Articles