Sublime Forum

Save settings without flush

#1

I’m trying to save some configurations in a setting file, but every time I use save_settings the file is flushed. There is a way to save a new parameter keeping the others?

When I do

sublime.load_settings(file).set(key, value)

it not stores the settings in the file, only keep it in memory

0 Likes

#2

What are you tring to do?

In the meantime, have an snippet.

name = 'Preferences.sublime-settings'
sublime.load_settings(name).set("font_size", 10)
sublime.save_settings(name)
1 Like

#3

I’ll try to be more specific

If I have a file with two setting already stored

{
  "key1": value1,
  "key2": value2
}
```

And if I use your example to store the font size

```
sublime.load_settings(name).set("font_size", 10)
sublime.save_settings(name)
```
 it will erase the previous value and the file will look like this

{
“font_size”: 10
}

 but I want:

{
“font_size”: 10,
“key1”: value1,
“key2”: value2
}


How should I do it?
0 Likes

#4

The hierarchy of settings is a tiny bit complex, and so you should probably read this page of documentation about it.

I’ll take a guess in the meantime.

  1. You manually defined file.sublime-settings outside Packages/User with some keys, and when you’re modifying and saving it with the API, a new file is created in Packages/User and contains only the key-value pairs you’ve modified.

You could in that case create a copy of your file and put it in Packages/User if it doesn’t exist already. Then, all the keys would be defined there from the start. Otherwise you’ll have to live with the fact that setting files in the User dir contains only the k-v pairs that were modified or added.

2 Likes

#5

I agree with @dubeg; I think you may be trying to work with settings outside of the Packages/User directory.

The only thing I have to add in this screenshot, which shows an interactive session wherein I load the settings, check the contents, then add a new setting and save it back, showing that it does indeed work.

The original file was in Packages/User/test_settings.sublime-settings but notice that I only specify the name and not any path to the file (as per the API documentation on load_settings()).

In this interactive example, the file was open above showing only the original two settings, but as soon as I did the save_settings() in the console, the file was reloaded (as mentioned in the console; once because the settings changed and once because the file was already open).

0 Likes

#6

Thanks guys,

I haven’t the file.sublime-settings outside of Package/User, but I have it in a sub-folder Packages/User/Folder I’ll try letting it in the user folder to check if it’s the problem.

@dubeg thanks for the link I’ll take a look

0 Likes