Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want that when i make create a particular file, suppose i create it in vim editor, the created file should get executable permission as well when it gets created . I want this as i am creating some python files and don't want to explicitly set permissions for the file so i want some way so that executable bit is set as soon i create a file with particular extension or with a particular editor.

share|improve this question

3 Answers

For vim, you have powerful scripting available. For example, in my .vimrc, I have:

" Stolen from http://www.debian-administration.org/articles/571
" Sets +x on stuff starting with the magic shebang.
au BufWritePost * if getline(1) =~ "^#!" | silent !chmod a+x <afile>

If you want to do it by filename only, instead of looking for the #! line, you could do:

au BufWritePost *.ext silent !chmod a+x <afile>     " untested

That article on Debian Administration has instructions for EMACS as well.

share|improve this answer
what is your autoread set to then? – dustin Mar 31 at 19:25
@dustin autoread is set (true/on/yes) in my vimrc (AFAIK, its just a boolean, its either on or off) – derobert Apr 1 at 15:45

As much as I like derobert's answer, it causes VIM to give me the following warning:

W16: Warning: Mode of file "test.sh" has changed since editing started

The following (somewhat longer) code solves that problem (requires a Python-enabled vim):

function! SetExecutableBit()
python <<EOD
import vim
import os
import stat
filename = vim.current.buffer.name
mode = os.stat(filename).st_mode
os.chmod(filename, mode | stat.S_IXUSR)
EOD
endfunction

autocmd BufWritePost *
    \ if getline(1) =~ "^#!" | call SetExecutableBit()
share|improve this answer
Ah, I probably don't get that warning as I have autoread set. – derobert Dec 26 '12 at 22:25
I'm confused that silences the warning, because it seems to do the exact same thing—just calling a python script instead of chmod... – derobert Apr 1 at 15:48
Yup. But for some reason, the Python function doesn't trigger the mode-change detection. Might be a quirk of my setup, though I believe I'm running a rather vanilla Vim on Ubuntu install. – Søren Løvborg Apr 2 at 12:25

put

umask 0544

in your ~/.bashrc

When you run without arg, you'll see:

0022

Suggestion of UNIX is 0022.But your case is ...

share|improve this answer
5  
This is terrible. That will make every file --w--w--w-. – bahamat Aug 21 '12 at 19:47
The default base fmask permission is 0666, so I don't think a separate chmod step is avoidable. – Thor Sep 17 '12 at 10:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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