Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Windows environment variables in Sublime Text settings files

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.

like image 721
Ned Martin Avatar asked Apr 14 '13 12:04

Ned Martin


People also ask

How do I change settings in Sublime Text?

Open the Sublime Text personal settings file: Mac OS X: Sublime Text 2 > Preferences > Settings - User. Windows: Preferences > Settings - User. Linux: Preferences > Settings - User.

How do I use .env files on Windows?

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

Can I use variables in .env file?

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.

Does .env file work in Windows?

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.


1 Answers

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()
like image 192
skuroda Avatar answered Oct 15 '22 02:10

skuroda