Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a file in read/write mode in Vim?

Tags:

vim

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.

like image 836
Tom McJones Avatar asked Sep 02 '10 15:09

Tom McJones


People also ask

How do I open a readonly file in vim?

You can use vim -R filename for opening the file in read-only mode (file is modifiable, but not writable) and vim -M filename for opening the file with modifiable set to off (file is neither modifiable, nor writable).

Which command opens an existing file in read only mode?

There are many modes for opening a file: r - open a file in read mode. w - opens or create a text file in write mode.


3 Answers

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

like image 191
Nentuaby Avatar answered Oct 24 '22 02:10

Nentuaby


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.)

like image 29
egbit Avatar answered Oct 24 '22 01:10

egbit


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

like image 1
xdhmoore Avatar answered Oct 24 '22 00:10

xdhmoore