I have a need to reference Windows environment variables from within Sublime Text 2 settings files (Package-Name.sublime-settings files), specifically %APPDATA%
and %TMP%
Is this possible, and if so, how?
For example, here is a line from one package setting, which needs to work on multiple users, so with different usernames:
"backup_dir": "C:\\Users\\Username\\AppData\\Local\\Temp\\SublimeBackup"
As an example, here is a problem I just had: I have an install of Sublime Text 2 which runs from multiple computers (i.e. I copy its data around to keep settings etc. up to date between multiple installs), but I have the below command:
{ "caption": "Backup to Server (Local to Server)", "command": "exec", "args": { "cmd": ["local-to-server.cmd"] } },
Unfortunately, the file "local-to-server.cmd" is relative to the currently opened file in Sublime Edit, so this command rarely works. What I need is:
{ "caption": "Backup to Server (Local to Server)", "command": "exec", "args": { "cmd": ["%APPDATA%\Sublime Text 2\Packages\User\local-to-server.cmd"] } },
Or some similar way of referencing a common location that I can then build a relative path from.
Open the Sublime Text personal settings file: Mac OS X: Sublime Text 2 > Preferences > Settings - User. Windows: Preferences > Settings - User. Linux: Preferences > Settings - User.
To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables. To reference a variable, use $varname , with a prefix '$' (Windows uses %varname% ).
env file is included to use, so it's easy to have a different configuration based on the environment your app is running on. This gives you the flexibility to have different variables for local, staging, production, and even different developers' machines.
env is loaded exactly. Go doesn't handle . env natively, you're probably using godotenv. It sets the env variables for the current process (if using it as a library) and might work on windows.
Thanks to @schlamar for the correction on settings. I didn't realize they persisted across the session. All my plugins use them locally, and I don't do any modification to them but that's good to know. Here's a plugin to expand the variables when ST loads. 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With