2

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.

2 Answers2

2

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:

#!/bin/bash
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.

Anthon
  • 79,293
  • 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
0

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.