Document AutoSave for Visual Studio

For many years I used CodeWright, and then SlickEdit, for all my editing. Both of those had a very useful feature:  Save Files on loss of focus.

As Visual Studio improved, I started using it more and more, and missed that autosave feature.  So, a few years ago, I wrote an AddIn for VisualStudio 2010 that saved all changed documents when VS lost focus.

I kept that AddIn up-to-date for VS2012 and VS2013, but read recently that AddIn support would be going away, and VSPackages was the way to enhance and extend VS.

Converting the AddIn to a VSPackage was pretty straightforward, except for one problem:  the autosave functionality didn’t work initially.  I found that this appears to be a difference in AddIn load behavior vs. VSPackage load behavior.  AddIns are initialized on load, while VSPackages are only initialized when certain conditions exist.  (This is done to minimize VS startup time by preventing all the extensions from trying to load at once.)

The load behavior is controlled by attributes decorating the class that extends Package.  I added the attributes for UIContext_EmptySolution and UIContext_SolutionExists, and the extension works as desired.  See this post for details in installing the extension.

(Note that you can get the same behavior in vim by adding this to your _vimrc:
‘ Save all files when gvim loses focus
au FocusLost * wa
“au” is the abbreviation for autocmd, and FocusLost is the name of the event to trigger it.

Similarly, setting autowrite saves changes when you switch buffers (:bn, for example).)

 

7 Comments

  1. Just thought I say thanks for this – this extension is probably the most useful one I have installed. My colleagues often commit stuff then notice that half of it was missing because it hadn’t been saved – never happens to me! I have shared this extension with them, but I think they feel installing it is a bit of hassle. Is there a reason it’s not available through the VS gallery?

    Anyway – thanks!

    • Glad you like the extension.
      “… they feel installing it is a bit of a hassle…” – do you mean setting up the additional extension gallery? Maybe I could create a powershell script to do that part; I’ll look in to it.
      As far as the VS gallery, I’ll investigate that, see what needs to be done to get an extension listed there.

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? * Time limit is exhausted. Please reload CAPTCHA.

Protected with IP Blacklist CloudIP Blacklist Cloud