The open_file
command only expands a few variables ($packages
and $platform
; possibly others but those are the only ones I’m aware of off the top of my head). So directly, that’s not something that you can do.
However it’s simple to make a plugin that adds that sort of functionality, such as the one below (see this video if you’re not sure how to use plugins):
import sublime
import sublime_plugin
import os
class OpenFileEnvCommand(sublime_plugin.WindowCommand):
"""
Drop in replacement for the open_file command that expands
environment variables as well as standard Sublime variables in all
command arguments.
"""
def run(self, **kwargs):
# Create a set of variables based on the current process
# environment and the standard Sublime variables
variables = dict(os.environ)
variables.update(self.window.extract_variables())
# Expand all variables and execute the base command with them
kwargs = sublime.expand_variables(kwargs, variables)
self.window.run_command("open_file", kwargs)
This creates a drop-in replacement for the open_file
command named open_file_env
that performs the same action as the built in command but also will expand environment variables as well (plus any variable that you can use in a sublime-build
file).
In use, you’d do something like the following, presuming an environment variable named MY_LIBRARY_PATH
was set to the appropriate location:
{
"caption": "Utilities",
"command": "open_file_env",
"args":
{
"platform": "Windows",
"file": "$MY_LIBRARY_PATH/utilities.js"
},
},
The syntax will always be to use $
for the environment variable (even on Windows, where that’s not usually how variables are expanded), and the names of the variables are based on the names that come from the process environment, so the case depends on how you set up the variable.
Note also that Sublime doesn’t see changes made to the environment while it’s running, so adding or changes variables requires you to restart it. Similarly on Linux the process environment is inherited from the parent process, which might be your window manager. In such a case you may also need to log out and in again to propagate the change (or just start Sublime manually from a terminal that has the new environment set).