6

There is a particular file on my computer that Vim insists on opening in readonly mode. I checked the permissions on the file (I have full control), and if I do :w!, writes happen just fine. However, whether I open the file with :e or from NERDTree, the file opens as readonly. All other files I open work just fine.

I just want to know how to turn off readonly mode on this file, before or after I open it.

I'm on Windows, using gVim

EDIT: Its probably not a permissions issue, since :w! works just fine.

Tom McJones
  • 61
  • 1
  • 1
  • 3
  • 1
    I don't know which specific Windows OS you're running, but if you're on Vista/Win7, have you tried running gVim as admin (right click and select Run as administrator), then see what happens when you try to save the file? – Dave Sep 02 '10 at 15:43
  • What type of file is it? Where is it located? Some file locations in the user directory will open in read-only, and w! won't even work. – Jess Sep 02 '10 at 15:47
  • @Dave: I'm on XP, and sadly, I can't run as administrator, since I'm at work. – Tom McJones Sep 02 '10 at 16:01
  • pls, see http://stackoverflow.com/questions/32659348/operation-not-permitted-when-on-root-el-capitan-rootless-disabled – A. Nikitin Feb 21 '16 at 11:11

7 Answers7

8

The mode that's annoying you is controlled by the readonly option. To clear it, type :set readonly! once you've launched the editor.

Nentuaby
  • 590
  • 6
  • 10
2

Actually, it can be a permission issue.

I've just hit this case on XP, with Administrator privileges. After making a file writable (readonly attribute clear, cygwin correspondingly shows it as writable), VIM still shows it as readonly.

In my case, the issue was under Advanced permissions. Looking under Windows Explorer at the file, right-click to get Properties, under the General tab the readonly bit is clear. Now going to the Security tab, it shows that I only have read permission. Clicking on the Advanced button, under the Permissions tab, with my user account selected, it shows I have Special permissions not inherited from anywhere, instead of "Full Control" inherited from the containing directory as I see for my other files.

One fix is to click Edit... to edit permissions in the Permissions tab. However, it is much simpler (at least in the case where you have Administrator privileges) to just do :w! in VIM as others have suggested. This fixes the file's permissions to be writable ("Full Control").

(I'm unclear how this particular file ended up in this "Special" read-only state. Leaving that as outside the scope of your question.)

egbit
  • 69
  • 3
1

The following added to your vimrc will cause vim to change the file to read-only once it is edited. It works by calling the windows attrib tool

"Change read/only files to read/write when they are edited
au FileChangedRO * !start attrib -r %
au FileChangedRO * :let s:changedRO = 1 
au FileChangedRO * :set noro

"Don't ask about the modified read-only file
au FileChangedShell * call s:HandleChangedROFile()

function s:HandleChangedROFile()
   if exists('s:changedRO') && s:changedRO == 1
      let s:changedRO = 0
      let v:fcs_choice='reload'
   else
      v:fcs_choice='ask'
   endif
endfunction

Inspired by: http://vim.wikia.com/wiki/Setting_file_attributes_without_reloading_a_buffer

xdhmoore
  • 8,935
  • 11
  • 47
  • 90
-1

The solution is pretty simple, this happens only if you don't have the write permission as a user.

go to the terminal and enter the following command:

1] chmod u+wx filename filename is the name of the file that you are trying to edit.

Guru
  • 2,739
  • 1
  • 25
  • 27
-1

Simply force the file to be written-to by using :w!. You should be golden

Braden
  • 313
  • 1
  • 6
  • 1
    He already said that he can do that fine, but he just doesn't want to have to do that. :) – Dave Sep 02 '10 at 15:42
-1

Maybe there is a vim setting in the file to make the file load as read only :

Check for a line like : (substitute the comment char of your file instead of //)

// vim:set ro:

Vim looks for these settings in the first or last 5 lines of the file.

Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
-1

Have you tried: Right click -> Properties -> see if the 'readonly' attribute is checked?

Nick Knowlson
  • 7,185
  • 6
  • 47
  • 63