Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there any setting for vimrc that changes the permissions that it sets on the file if it's creating a file? For example, if I create a file now it's 664, but I would like it to default to 755. Anyone know of the setting that I can put in the vimrc to do so?

share|improve this question

5 Answers 5

up vote 3 down vote accepted

You can't do it in your .vimrc, but if you set your umask properly in the environment where Vim is launched, you can (mostly) accomplish what you want. See Wikipedia's umask article.

Umask will not default to executable on files (unless the program creating the file asks for execute permission on creation), but it will on directories. However, even if you could, jjlin is correct, you should never default to executable on files; change it only on an as-needed basis. This is easily achieved by doing something like ":!chmod a+x %" from within Vim.

share|improve this answer

Files should not default to being executable, so even if vim had such a feature, you shouldn't be using it. Set the executable bit on a case-by-case basis.

share|improve this answer

As others have written, you shouldn't just set execute permission on every file you write. On the other hand, if you are creating a new file in some scripting language, you presumably want to execute it, so why not save yourself a few keystrokes.

To automatically make new shell scripts executable by you, put the following in your ~/.vim/after/ftplugin/sh.vim file.


au BufWriteCmd <buffer> call s:BufWrite(expand("<afile>"))

function s:BufWrite(file)
    if v:cmdbang == 1
        let bang = "!"
    else
        let bang = ""
    endif

    if glob(a:file) != ""
        exe 'w'.bang a:file
    else
        try
            exe 'w'.bang a:file
        finally
            exe '!chmod u+x' a:file
        endtry
    endif
endfunction

share|improve this answer

As already pointed out, you should not ever create files with the executable bit set by default. The standard mechanism for setting so called file creation masks, is controlled by umask. The umask command can be used to view and set these values for the current environment. However, you'll only be needing to set the executable bit on your files whenever this really is necessary - when writing scripts and such. So you'd be better off creating i.e. a bash wrappy function for doing exactly what you want:

vimx() { vim "$@" && chmod +x "$@"; };

Stick this in your ~/.bashrc file and run source ~/.bashrc. Now, whenever you run vimx the file will be saved with the executable bit set.

If you just want a test run on an opened file (without +x permissions) you can simply run the following command from inside vim to test your script (here bash script):

:!bash %
share|improve this answer

If you are interested in making newly created Unix-style #! scripts files executable when you first save them, then you might find Tim Pope’s vim-eunuch plugin useful. It includes some (undocumented) “autocommands” that will do a chmod +x on a new file whose first line starts with #! (e.g. #!/bin/sh) immediately after saving it for the first time.

Undocumented in the sense that they are not mentioned in the README file.

share|improve this answer

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.