When I create a new script, I always have to manually add executable permissions with chmod +x foo.sh. My default permissions are set to 644, and but I'd like any file with extensions like .sh .py to automatically be set to as soon as they're saved so that I don't have to chmod them every time. I've seen several articles relating to how to change default permissions for all files in a folder or directory, but none discussing differentiating by file extension.

I use Vim. I would accept an explanation of what to put in my .vimrc to accomplish this as an answer.

  • That's because Linux doesn't really differentiate files by file extension, unlike Windows. Maybe a better option would be to have whatever editor you use chmod them after the first save. – Seth Feb 19 '15 at 15:52
  • Related: unix.stackexchange.com/questions/1314/… – Seth Feb 19 '15 at 15:53
  • @Seth Good to know. I would accept an explanation of what to put in my .vimrc to accomplish this as an answer. – Ben Lindsay Feb 19 '15 at 15:59
  • If you use good editor just customize it so that if it creates the file with certain extension then +x is automatically added to permissions. – jimmij Feb 19 '15 at 16:10
  • 1
    Related: vim create file with +x bit – Mark Plotnick Feb 19 '15 at 16:47

If you automate things with scripts and make scripts on a regular basis, you should automate the script creation...with a script.

So instead of calling:

vi some_new_program.py

you should have a script newpy:

echo '#!/usr/bin/env python' > "$1"
echo '# coding: utf-8' >> "$1"
echo '' >> "$1"
chmod +x "$1"
vi +3 "$1"

Of course you want to check in the script if you are about to overwrite something etc. You could combine the script for generating new shell scripts or python programs and have it decide based on the extension what to write in the file etc.

  • This is a good idea, but I'd prefer to keep the same procedure for creating new files. I already have a header file that vimrc pulls from to get the right header, and if I could just add the line to vimrc that changes permissions then I could keep doing things the way I've been doing them.. – Ben Lindsay Feb 19 '15 at 16:20

The best answer to my own question is found in the answer to another post: vim: create file with +x bit. That makes this question essentially a duplicate.

I think I'll delete this question unless someone sees a reason to keep it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.