Use Windows environment variables in Sublime Text settings files

I need to reference the Windows environment variables from the Sublime Text 2 settings files (Package-Name.sublime-settings files), in particular %APPDATA%and%TMP%

Is this possible, and if so, how?

For example, here is a line from one package parameter, which should work for several users, therefore with different user names:

"backup_dir": "C:\\Users\\Username\\AppData\\Local\\Temp\\SublimeBackup"

As an example, here is the problem that I just ran into: I have a Sublime Text 2 installation that works from multiple computers (i.e. I copy my data to save settings, etc. between multiple installations), but I run the following command:

{ "caption": "Backup to Server (Local to Server)", "command": "exec", "args": { "cmd": ["local-to-server.cmd"] } },

Unfortunately, the file "local-to-server.cmd" refers to the current open file in Sublime Edit, so this command rarely works. What I need:

{ "caption": "Backup to Server (Local to Server)", "command": "exec", "args": { "cmd": ["%APPDATA%\Sublime Text 2\Packages\User\local-to-server.cmd"] } },

Or a similar way of referencing a common location, which I can then build a relative path.

+5
source share
2 answers

Thanks to @schlamar for fixing the settings. I did not understand that they persisted in the session. All my plugins use them locally, and I do not make any changes to them, but it is useful to know. Here is a plugin for expanding variables when loading ST. It should work in both ST2 and ST3.

import os
import sublime

VERSION = int(sublime.version())

def expand_settings():
    expand_settings = {
        "<setting file names>": [
            "<setting keys to expand>"
        ]
    }
    for filename, setting_keys in expand_settings.items():
        s = sublime.load_settings(filename)
        for key in setting_keys:
            value = s.get(key)
            s.set(key, os.path.expandvars(value))

def plugin_loaded():
    expand_settings()

if VERSION < 3006:
    expand_settings()
+2
source

@skuroda . load_settings. :

s = sublime.load_settings('Preferences.sublime-settings')
s.set('test', 'x')
s = sublime.load_settings('Preferences.sublime-settings')
print (s.get('test'))  # prints x

, x ( , ).

, os.path.expandvars, .

+1

All Articles