Sublime Forum

[Solved][Question] How to edit Preferences.sublime-settings file in my package?

#1

1. Briefly

I have Preferences.sublime-settings file in a Packages/SashaSublime folder. How I can simply edit it?

2. Detail

If I want to edit SashaSublime Preferences.sublime-settings file, I need delete file → create file with the same name → edit and save file → if I need to edit Preferences.sublime-settings again, I need repeat these actions again.

May I simply edit and save my SashaSublime Preferences.sublime-settings file like usual files?

3. Did not help

  1. I try to use packages for toggle read-only file protection. No effect.

4. Do not offer

  1. Yes, I know, that I need edit User settings files, not Default. But I want quickly edit my package.

Thanks.

1 Like

#2

I don’t see why you couldn’t edit the file directly, unless you are talking about the package being a .sublime-package file, in which case you should use PackageResourceViewer.

1 Like

#3

@FichteFoll,

unless you are talking about the package being a .sublime-package file

No, my SashaSublime package are extracted, and I can edit any file of my package except Preferences.sublime-settings.

Thanks.

1 Like

#4

If I understand you correctly you want to transfert the settings of your package into the user settings. You can add that into a command (you may want to ask for permission if you publish your package):

# load the settings to transfer
res = sublime.load_resource("Packages/SashaSublime/Preferences.sublime-settings")
my_settings = sublime.decode_value(res)

# transfer the settings to the user settings
user_settings = sublime.load_settings("Preferences.sublime-settings")
for k, v in my_settings.items():
    user_settings.set(k, v)

# save the user settings
sublime.save_settings("Preferences.sublime-settings")
2 Likes

#5

@r-stein, may you write more details, where I need to use your code :unamused:?

Thanks.

1 Like

#6

To toggle readonly mode, just open up the console, and paste

view.set_read_only(False)
1 Like

#7

Not helped for me. I get same behavior.

Thanks.

1 Like

#8

Sorry it seems I misunderstood the question.
How do you open the file?

1 Like

#9

@r-stein,

How do you open the file?

Yes, my current question — how I can open file, but your answer made me very interested :smile:.

You said, that I can transfert the settings of my package to the user settings. But where I have to insert your code?

Thanks.

1 Like

#10

If you use PackageResourceViewer you should be able to edit every file without problems.

This is the full code to transfer the settings. You can just put it inside a command and ask for permission before:

import sublime
import sublime_plugin


class TransferSashaSettingsCommand(sublime_plugin.WindowCommand):
    def run(self):
        ans = sublime.ok_cancel_dialog(
            "Do you really want to transfer the settings? "
            "(You cannot undo this operation.)"
        )
        if not ans:
            return
        # load the settings to transfer
        res = sublime.load_resource(
            "Packages/SashaSublime/Preferences.sublime-settings")
        my_settings = sublime.decode_value(res)
        # transfer the settings to the user settings
        user_settings = sublime.load_settings("Preferences.sublime-settings")
        for k, v in my_settings.items():
            user_settings.set(k, v)
        # save the user settings
        sublime.save_settings("Preferences.sublime-settings")

Then just use the transfer_sasha_settings command in a keybinding or somewhere else.

2 Likes

#11

@r-stein,

This is the full code to transfer the settings. You can just put it inside a command and ask for permission before:

Great solution! This script rewritten, but not delete data into User/Preferences.sublime-settings file.

If you use PackageResourceViewer you should be able to edit every file without problems.

I get same behavior.

Perhaps, it bug?

Thanks.

1 Like

#12

I’m not sure I understand you right, so to have the same base:

PackageResourceViewer allows you to view files in package (that are usually zipped in a .sublime-package). That’s the item Open Resource. So, you can’t edit this file, (or you’d have to re-write the entire .sublime-package)

The solution to edit a file in a package is to extract it. It’s the item Extract Package. What this is doing is converting a .sublime-package into a regular folder in the $packages directory. And now you can edit the files in the package. If the folder has the same name as the .sublime-package file (it’s the case by default), it’ll override it. So, for example, Open Resource will now open the extracted files, so you’ll be able to edit them.

If you don’t want package control to automatically transform your package to .sublime-package when installed, you can add a .no-sublime-package at the root of your package (but you should try avoiding that).

1 Like

#13

but my normal workflow is:

  1. Open Command Palette
  2. PackageResourceViewer: Open Resource
  3. Choose package
  4. Choose file
  5. Edit file
  6. Save file

File is now saved as an override, if the package was a .sublime-package file, or overwrites it if it had a .no-sublime-package file in it and thus was never a .sublime-package file.

I never use Extract Package unless I plan to override every file in that package.

1 Like

#14

@math2001, @kingkeith, @r-stein, @FichteFoll, steps to reproduce:

  1. Create new folder SashaTest in Data/Packages folder.
  2. In SashaTest folder create file Preferences.sublime-settings.
  3. Write anything in Preferences.sublime-settings file, for example, these preferences.
  4. Save and close Preferences.sublime-settings file.
  5. Reopen Preferences.sublime-settings file use any method.
  6. I get behavior like in GIF.

Thanks.

1 Like

#15

following those steps, I am able to edit the preferences after re-opening the file. Maybe you have some plugin installed that is marking it as read-only? I would have expected math’s fix to work, though, unless somehow the read-only mode gets reapplied instantly…

what happens if you type view.is_read_only() Enter in ST’s console?

1 Like

#16

1. Solution

I solved my problem. I had set "protect_default_settings": true, in Preference-Helper.sublime-settings file. I spent 4 hours of my time :rage:.

2. How I debug it

1. Finding reason

@kingkeith, this problem don’t reproduce for me in Sublime Text version without plugins and user settings. But when I disable all plugins and remove user settings for my working Sublime Text directory, I reproduce the problem.

I begin to find in Sublime Text files, where may be reason of the problem. I find in Session.sublime-session file lines:

{
    "file": "/D/Sublime Text 3 x64/Data/Packages/SashaSublime/Preferences.sublime-settings",
    "settings":
    {
        "buffer_size": 732,
        "line_ending": "Windows",
        "read_only": true
    }
},

I don’t understand, why "read_only": true.

2. Removing session and workspace

I disable all Sublime Text packages → I delete Session.sublime-session file → I close Sublime Text → I delete SashaProject.sublime-workspace (workspace of my project) file. I need to delete Session.sublime-session file, because, apparently, sublime-workspace file generate from sublime-session file. I open Sublime Text → I create new project → I don’t reproduce the problem in new project. I restart Sublime Text → I don’t reproduce the problem.

3. Disable/enable packages

Now I enable 50% of my packages → I restart Sublime Text → I don’t reproduce the problem. I enable 50% of the remaining packages and so on… I reproduce the problem, I delete Session.sublime-session file → I create new project → I reproduce the problem in the new project → I disable 50% packages, enabling in my last action, and so on… I find conflicted package — PreferencesHelper.

Thanks.

2 Likes