Sublime Forum

New files saved without executable permissions

#1

Whenever I save a new file, it gets saved with -rw-r–r--. I write a lot of shell scripts so this is incredibly annoying. Is there a way to set what permission a new file has? I don’t remember setting anything and I don’t know why I would set it to that, but I’d be surprised if it saves files that way by default.

I’m using Mac OSX 10.10.5, with Sublime Text 3 3126.

1 Like

#2

You can change the permission of the file with a script after you’ve saved it. I’m on mobile right now but look at EventListener.on_post_save in the API docs.

1 Like

#3

Similar question was asked here:

I’ve adjusted the script for you:

import sublime_plugin
import os
import stat

class MakeExecutableIfNot(sublime_plugin.EventListener):

    def on_post_save(self, view):
        if not view:
            return
        file_name = view.file_name()
        if not file_name:
            return

        # Only modify the executable permissions when saving shell scripts.
        if not view.match_selector(0, "source.shell"):
            return

        # Add executable permission to user.
        st = os.stat(file_name)
        os.chmod(file_name, st.st_mode | stat.S_IEXEC)
        print("added executable permission to", file_name)

Go to Tools -> Developer -> New Plugin and save this in your Packages/User directory as MakeExecutableIfNot.py (or whatever you want to call it).

2 Likes

#4

You rock! Works great.

So… saving files without executable permission is the expected behavior of ST3? For an application so useful for coders, this seems odd that you would have to write a plugin to make that happen.

Whatever the case, there is now a documented way to make it happen! Thanks for the help!

1 Like

#5

If I had to guess, I would say that the reason it doesn’t do that for all files all the time is because it’s not needed by all developers all the time. For example, as a C developer I hardly ever need my C source files and headers to be executable, and doing so would make the output of e.g. ls more confusing (although that is of course a matter of taste).

I’ve never looked into it, but Sublime may honor the umask that you have set in your bash profile, although since on MacOS GUI apps don’t get launched by the same parent process as terminal apps, it may not. Possibly something to look into, though.

3 Likes

#6

If I had to guess… It would be because ST uses the default system regular file permissions mask, which by default -I’d be prepared to bet- on your version of OSX results in regular files being created with 644 (-rw-r--r--) permissions.

Open a terminal and type 'touch mattst', then use 'ls -l' to look at the file permissions before deleting 'mattst'. If they are not 644 (-rw-r--r--) then I’m wrong… but I won’t be. :slight_smile:

3 Likes