0

This question already has an answer here:

$ ls sess.vim -lh              
-rw-r--r-- 1 root root 11K Feb 26 18:52 sess.vim

I want this file to be readable for everyone and writable by no one (except by root). Thus I set its permissions to 644 and ownership to root:root.

$ echo "text" >> sess.vim      
zsh: permission denied: sess.vim

Seems fine. After some changes in vim I do :w! (force write) and the file is saved successfully. Now:

$ ls sess.vim -lh
-rw-r--r-- 1 MY_USERNAME users 11K Feb 26 19:06 sess.vim

Wt.. Why? How?

marked as duplicate by Community Feb 26 '16 at 17:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    sounds like you replaced the file and over wrote the old one with a new one, so rather than modifying you deleted and recreated. keeping the same file permissions but exchanging the username for the creator which is now technically your username – TheHidden Feb 26 '16 at 17:10
6

Using :w! in vim is similar to the following:

echo 'test' > sess.vim.temp
mv sess.vim.temp sess.vim

The mv commands only cares about the directory permissions, the permissions of the file are not relevant. This is because you are modifying the directory, not writing to the file. To accomplish your goal, you will also need to adjust the permissions of the directory the file resides in.

  • and even more, you can use :w !sudo tee % to write to directory you are not an owner ) – Ivan Temchenko Feb 26 '16 at 17:28
1

This is working because the user and group ownership of the parent directory is your-username:your-group-name

If a user is the owner of a directory and has write permission for that directory he is allowed to remove any file contained in it (regardless of the file's permissions). I suspect vim unlinks (removes) the file it edits and writes a new file with the same filename when :wq! is given.

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