Sublime Forum

Setting & getting the value assigned to a key in a .sublime-settings file

#1

I have a command that sets a value to a key in a .sublime-settings file and another command that retrieves the said value. Something like the following:

import sublime
import sublime_plugin

class Example1Command(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings("name of the settings file")
        settings.set("key", "value")
import sublime
import sublime_plugin

class Example2Command(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings("name of the settings file")
        print(settings.get("key"))

Assume my .sublime-settings file is of the following structure:

{
    "key": ""
}

However executing Example2Command doesn’t print anything. What am I doing wrong ? Why is it not setting the value ?
Files for both commands & the settings file are in the same directory (which is under Packages folder)

0 Likes

#2

It seems to work OK when I do it; I assume you’re not getting any errors in the console of any sort? You didn’t mention, but I also assume that you ran example1 first; otherwise I would expect the setting value to be nothing when executing example2.

Just to clarify, the code I tested was the following (though having the two commands in separate files should have no effect on anything):

import sublime
import sublime_plugin

class Example1Command(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings("Sample.sublime-settings")
        settings.set("key", "value")


class Example2Command(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings("Sample.sublime-settings")
        print(settings.get("key"))        

The output from the console:

>>> view.run_command("example1")
>>> view.run_command("example2")
value
1 Like

#3

Also note that settings set like this aren’t persisted on disk, so if you restart ST between calling the first and the second commands, the setting will be reset to the previous value (i.e. not set). Use sublime.save_settings if you need this.

1 Like

#4

Yes. I had executed Example2Command after Example1Command. The only thing that’s actually different from the example I have given & what I am trying to achieve is that the key field is an empty list. So something like

{
    "key": []
}

And Example1Command would be

import sublime
import sublime_plugin

class Example1Command(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings("Sample.sublime-settings")
        list_of_values = settings.get("key")
        settings.set("key", list_of_values.append("value"))

Thought that any answer to my simple example might help me figure out what to do.
Apparently when I execute Example2Command after Example1Command, I get None in the console.

0 Likes

#5

The list.append() method doesn’t return the list after the modification, it returns None:

>>> x = []
>>> print(x.append("something"))
None

To do something like this, you need to do the append separately:

class Example1Command(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings("Sample.sublime-settings")
        list_of_values = settings.get("key")
        list_of_values.append("value")
        settings.set("key", list_of_values)
2 Likes

#6

Sorry for the late reply. Was just able to get back to this due to a busy week. Your solution works but when I open .sublime-settings I don’t see them persisted (of course FichteFoll has answered that) but if they aren’t persisted in .sublime-settings, then how exactly does Sublime find the value after setting it ?

0 Likes

#7

In any given run of Sublime, the first time sublime.load_settings() is called to load a settings file, the data is loaded into memory and the resulting object is cached and returned back. From that point forward any time you or any other plugin calls load_settings() with the same settings file name, that same settings object is returned.

One of the benefits of that is that it’s very efficient to call load_settings() from within any command that needs access to settings data; only the first call actually needs to look at the disk. If you instead keep the returned object, you’ll notice that even if other commands modify the settings, they’re reflected in the object that you originally got.

This ensures that whenever anyone changes a setting, everyone that’s accessing the setting knows about it without having to take extra steps (otherwise if you were to run_command() another command, you would have to make sure that command didn’t modify the settings while it was running, for example).

Calling sublime.save_settings() will persist to disk (in the User package) the settings as they now appear so that they will be remembered for a future session, but within the same session the data is still loaded in and available.

2 Likes