As I learned recently, these variables don’t expand outside of build systems. In fact I believe that the documentation I referenced there has been amended accordingly.
In the case of that particular post I made my own simple command that did what I wanted. Something similar could work for you as well:
import sublime_plugin
# Make an executable of the current file based on an extension and then
# execute it
class RunCurrentFileCommand (sublime_plugin.WindowCommand):
def run(self, extension):
# Get vars and make sure that we have all of the ones we need
vars = self.window.extract_variables ()
if "file_base_name" in vars and "file_path" in vars:
# The executable is the current file with a new extension
executable = vars['file_base_name'] + extension
# Make the working directory be the path of the current file
working_dir = vars['file_path']
self.window.run_command ("exec", {
"cmd": [executable],
"working_dir": working_dir
})
else:
sublime.status_message ("Error: No file")
Save this in (for example) run_current_file.py
in your User Package (Preferences > Browse Packages
) and the command will become available using a binding similar to:
{
"keys": ["ctrl+shift+x"],
"command": "run_current_file" ,
"args": {"extension": ".exe"}
}
As written you need to specify the extension to append to the current filename. If you’re not going for cross platform action this could be modified to not require the extension argument and just use a hard coded extension.
As a variation, you might want to use the variables that give you the full path to the file less it’s extension instead of setting the working directory to be where the file is, if you need the working directory to be some other path.
Note that the name of the command is derived from the name of the class in the Python code (RunCurrentFileCommand
) and not from the name of the file you save the code in, if you want to modify that as well.