views:

204

answers:

4

The subject says it all. Is there an easy way to toggle the editability of a buffer in Visual Studio? This would be similar to the toggle-read-only command in Emacs.

I am not looking to change the file attribute ... just whether I can edit the file while it is open in Visual Studio. I am using 2008 and 2005.

Why would I want to do this? I tend to have several files open at the same time .... for days at a time sometimes (perhaps a bad habit) and I have +/- a char or few here and there without meaning to or noticing ... also worried about "the cat walking across the keyboard"

Besides ... an "ancient" code editor like emacs has it :) and I grew to expect the feature.

TIA!

A: 

Not that I am aware of. Why do you want to control the editability of the file - do you want to prevent accidental edits?

Stephen Doyle
A: 

I'm not aware of anything that will quickly achieve what you're looking for. Furthermore, I'm not really sure why you would need such a thing. Typically I use subversion to tell me which files have been changed and where they have been modified that way I can revert anything that doesn't belong.

Can you expand on your question a little to let us know what your usecase is?

If you really need to toggle readonly....perhaps you can:

  1. Right click on the file
  2. Select Open Containing Folder
  3. Right click on the file and choose properties
  4. Check the readonly checkbox
mezoid
+1  A: 

You can use this piece of vba

Public Sub ToggleReadOnly()

    Dim doc As Document
    doc = DTE.ActiveDocument

    If (doc.ReadOnly) Then
        doc.ReadOnly = False
    Else
        doc.ReadOnly = True
    End If

End Sub

Note: the msdn documentation specifically mentions that the property ReadOnly shouldn't' be used explicit but I verified that this works for me on vs.net 2005.

I also verified that the actual file attribute isn't changed.

chollida
A: 

Start Tools->Macros->Macro IDE. Add new module (described here in details) and define there procedure as follows:

Imports EnvDTE

Public Sub SwitchReadOnly()
    DTE.ActiveDocument.ReadOnly = Not DTE.ActiveDocument.ReadOnly
End Sub

Assign macro to keyboard key(described here in details). That's all.

Kirill V. Lyadvinsky
Your answer looks awfully familiar:)
chollida