Sublime Forum

$file not recognized as a part of exec in shortcut

#1

Hello all,

I’m trying to add a shortcut for running the current file in the other tool and I add this to my Default (Windows).sublime-keymap:

{ “keys”: “ctrl+shift+alt+d”], “command”: “exec”, “args”: { “cmd”: “C:\Program Files (x86)\Decoda\Decoda.exe”, “$file”], “shell”: true } }

but when I press the shortcut the app opens without file and I see in console that $file was not substituted:

Running C:\Program Files (x86)\Decoda\Decoda.exe $file

Am I doing something wrong?

Thanks in advance,
Anton.

0 Likes

Not possible to expand path vars in exec command?
#2

I believe the variable substitution occurs before it gets passed to the exec command. You will need to write a simple plugin to convert the variables before passing it to the exec command. Hope that helps.

0 Likes

#3

Yeah, had to come up with this:

import sublime, sublime_plugin, subprocess

class RunDecodaCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        if view and view.file_name():
            command = "C:\Program Files (x86)\Decoda\Decoda.exe"
            result = subprocess.call([command, view.file_name()], shell=True)
            if result != 0:
                view.set_status('run_decoda', "run_decoda: process %s launching error %d" % (command, result))
            else:
                view.set_status('run_decoda', "run_decoda: process launched...")

            sublime.set_timeout(self.clear,10000)
        else:
            sublime.error_message("No file to open!")

    def clear(self):
        view = self.window.active_view()
        view.erase_status('run_decoda')

and launching key binding:

{ "keys": "ctrl+shift+alt+d"], "command": "run_decoda" }
1 Like

#4

The easiest solution I found for this is to use the build system as a proxy for exec, which then lets you use $file, $file_name, etc.

Go to Tools > Build System > New Build System, copy and paste your old build system into here, or just edit your old build system, and then add a variants section, give it a name, and all the other commands you want. Then add a new keyboard shortcut and add an args with the variant name.

All in all:
David-Go.sublime-build

{
  "shell": "true"],
  "cmd": "sublime-go-build '$file' '$file_name' '$file_path'"],
  "selector": "source.go",
  "variants": 
    {"shell": "true"],
      "cmd": "sublime-go-test '$file' '$file_name' '$file_path'"],
      "name": "Run"}]
}

User/Default (Linux).sublime-keymap

	{ "keys": "ctrl+b"], "command": "build" },
	{ "keys": "ctrl+shift+b"], "command": "build", "args": {"variant": "Run"} }

Now when I press ctrl+b with a .go file open it runs the build command which executes my bash script “sublime-go-build” which is in my system’s $PATH with three arguments, $1=$file, etc. If I press ctrl+shift+b it does the same thing with “sublime-go-test” instead. You can add as many variants as you want and set them to whatever keyboard shortcuts that you want. One advantage is that now you can run this only on the language files you want, one disadvantage is that you have to add this to every languages sublime build file.

2 Likes