Sublime Forum

Can I use env variable in paths of .sublime-commands?

#1

Hi everyone. I’m sharing a Packages folder between my 3 computers using a Dropbox and creating a symlink using a mklink on Windows. Everything works fine except for .sublime-commands file that uses absolute paths that aren’t the same on the machines.

{
"caption": "Utilities",
"command": "open_file",
"args":
    {
        "platform": "Windows",
        "file": "/D/Path/To/My/File/libraries/utilities.js"
    },
},

Can I somehow set /D/Path/To/My/File to an env variable or something else that’d help me to use one menu on machines with different paths to libraries folder?
There was a similar topic from 2012, maybe anything changed since then?
Thank you.

0 Likes

#2

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).

4 Likes

#3

Terence, thank you so much for your detailed and informative answer! Everything works perfectly now :smiley:

0 Likes

Use Windows variable in key bindings