Sublime Forum

Help with toggling non-boolean setting using keybind

#1

Hey there, I’m wanting to have a key bind to toggle open_files_in_new_window between never and always

Here’s what I’ve got in my keybind settings

{
    "keys": ["f8"], 
    "command": "set_user_setting", 
    "args": {
        "file": "Preferences.sublime-settings",
        "setting": "open_files_in_new_window",
        "value": "never"
    },
    "context" :
    [
        {
            "key": "setting.open_files_in_new_window",
            "operator": "regex_match",
            "operand": "always"
        },
    ]
},
{
    "keys": ["f8"], 
    "command": "set_user_setting", 
    "args": {
        "file": "Preferences.sublime-settings",
        "setting": "open_files_in_new_window",
        "value": "always"
    },
    "context" :
    [
        {
            "key": "setting.open_files_in_new_window",
            "operator": "regex_match",
            "operand": "never"
        },
    ]
}

If I remove the context and keep one, it works to change the setting, but I want one button to toggle it. What’s not working here?

0 Likes

#2

I suspect the context resolution logic for setting. keys only supports equal/not_equal operators and boolean operands. There is a plugin implementing an extended version you could try instead of set_user_setting:

0 Likes

#3

No that’s no true, because if I use the keybind setting without the context, it works:

{
    "keys": ["f8"], 
    "command": "set_user_setting", 
    "args": {
        "file": "Preferences.sublime-settings",
        "setting": "open_files_in_new_window",
        "value": "never" <-- swap this between "always" and "never" and works it both ways 
    }
}

However, considering all I’m needing is a toggle, the extension you listed is actually a perfect alternative, cheers!

0 Likes

#4

So for those of you who may come here looking for answers, I’ve got it solved. The extension option is close but STealthy-and-haSTy’s solution isn’t functional enough; specifically it doesn’t persist across different views/windows, nor reloads (you can see how that might be a problem with my specific use case).

I created a gist which is just a modification of STealthy-and-haSTy’s py file (so props to him, and to kingkeith for pointing it out!). It’s available at https://gist.github.com/torrentails/e8b6…4c4b, just drop the file in your Packages/user and read through it’s comments to see how to use it.

1 Like

#5

@torrentails, thank you for doing this! Just to make sure I understand, the script does not change the Preferences.sublime-settings file, correct? That means once I close and reopen ST, the settings are back to the ones set in the Preferences.sublime-settings file, right?

0 Likes

#6

No, it does actually write to the preferences file, it does persist across reloads. Ironically, for the use case where you want the setting to be temporary, then STealthy-and-haSTy’s solution posted above is actually what you’re probably looking for :smile:

0 Likes

#7

I do want the plugin to change global settings permanently, but unfortunately I do not see any changes to the user preferences. I am playing around with open_files_in_new_window setting. Every time I press the hotkey, I see the message in the console that the option was changed, and it does work (i.e., it changes from opening a file in the same window to a different window, and back again). However, it does not change the user preferences file. After I close ST, it goes back to the default never. I am using Windows 11, ST 4200. Any ideas what could be preventing the user preferences files from being changed?

0 Likes

#8

Changed settings can be persisted by calling sublime.save_settings().

1 Like

#9

@deathaxe, thank you for the pointer! I added sublime.save_settings('Preferences.sublime-settings'), and now it is working. Thank you very much!

0 Likes

#10

Thanks @deathaxe, was working peacefully for me, but I also realized that I had a Sublime window open on another desktop, so that was what gave me the illusion of persistence :sweat_smile:

I’ve updated the gist with an optional temporary argument, that you can use to achieve the desired result; no hidden windows on other desktops necessary :grin:

1 Like