Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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 '13 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 '13 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)

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 '13 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 '13 at 12:25

Your Answer


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.