Thursday, September 1, 2016

Saving Time and Reducing Repetitive Typing in Visual Studio

By Steve Endow

Writing code in Visual Studio often involves a lot of repetitive typing.  Things like variable declarations, method stubs, and properties are often 95% the same, except for a variable / property / method name.

There are some tools in Visual Studio that can help save time, like code snippets that I have discussed previously, but code snippets are a bit of a hassle to define.  And there are many simple things that you have to type repeatedly, or that need variable values, that just aren't well supported by Visual Studio automation or intellisense.

From what I've read, macros were removed from Visual Studio, so I was unable to find any native features that could help me easily automate simple text statements.

[UPDATE: Tim Wappat pointed out that there is a VS Extension that apparently adds macro support back to VS.  This might be a good alternative for your use case, but I'm guessing it's overkill for my needs. https://visualstudiogallery.msdn.microsoft.com/d3fbf133-e51b-41a2-b86f-9560a96ff62b ]

One simple example would be my code comments.


I like to use a format of the date, then my name, then the comment.  Since I usually add a comment with nearly every change I make, I am often adding dozens of comment lines throughout my code.

Sure, it isn't terribly difficult to type:

//9/1/2016: S. Endow:

But after you've typed it thousands of times over a few years, it becomes obvious that there isn't much value in typing it all out.  So I finally got sick of typing it and looked into apps that provide shortcut keys to insert text values.  I wanted something that could automatically type out the comment line and insert the current date.

There are HUNDREDS of such apps out there, but I found that they each tend to focus on a particular feature set, and many of them didn't do exactly what I wanted.  So I had to sift through dozens to find those that allowed me to insert text when I pressed a keyboard shortcut.  Kind of like pre-defined clipboard entries that I could map to custom keystrokes.

The three that I have tried so far are:

1. QuickTextPaste (free)
2. PhraseExpress ($49 - $219 per copy)
3. FastKeys ($9.99 per copy)

I used QuickTextPaste for quite a while.  While it can work fairly well, it is very rough, with bizarre menus, finicky interface, and some frustrating quirks that I had to tolerate.  It's great that it is free, but I was willing to pay for something better.

I then tried PhraseExpress. It works fine, but it has a bug or two, and the price was way too expensive for me, as I want to use it on my desktop, laptop, and multiple virtual servers, and they require a license for every machine where it is installed.

But today, after searching for the 20th time for similar apps, I tried FastKey.  At only $9.99, it's an amazing bargain, and at that price, I'm happy to buy 5 or 6 licenses to use on my various machines.

So I installed it on one of my Window Server 2012 R2 virtual servers and tested it with Notepad.  It worked great, and the features are pretty phenomenal, especially for the price.

But when I tried to use it in Visual Studio 2015, it didn't work.  It wasn't detecting the keyboard shortcuts.  I then tried VS 2013 on the server, but that didn't work either.  This is an issue I experienced with QuickTextPaste on this same server, which prompted me to try a new app.

I then tried it on a Windows Server 2008 R2 and it did work in VS 2013.  I then tried it on a Windows 10 machine with VS 2015, and it worked there, to my surprise.

On the Server 2012 R2 machine, I tried turning UAC down to the lowest level, but that didn't help.

So there was something about Server 2012 R2 that was preventing it from working in Visual Studio 2013 and 2015.  I posted a note on the FastKeys support forum, and within a few hours received a response suggesting that I try using Run As Admin with FastKeys.  Duh.  I should have thought of that, but it didn't occur to me, since I had tried turning "off" UAC.

Sure enough, that worked.  When FastKeys is Run As Admin, it does detect the keyboard shortcuts used in Visual Studio, and inserts the desired text.


But there is one more catch.  Windows Server 2012 R2 does not allow a startup app to be run as administrator.  When I set the EXE to run as admin, FastKeys did not launch when I logged into the server.

After some searching, I found this thread recommending using Task Scheduler to launch the app at login.  Apparently Task Scheduler can launch an app with elevated privileges and bypass UAC completely.

https://superuser.com/questions/920697/how-to-run-a-program-with-administrator-privileges-on-user-login-under-windows




With that crazy maze of indirect processing setup to bypass UAC restrictions, FastKeys finally launched with admin rights when I logged in and it worked in Visual Studio.

Mission.  Accomplished.

You can potentially use the text expander feature, but you have to be careful that none of your text shortcuts conflict with the thousands of potential Visual Studio intellisense options--which is not easy.  One TextKeys example uses a comma prefix, so ",ccc" would trigger an action.  That's probably what you'll need to do if you use text expander with Visual Studio.  But for now I'll probably stick with keyboard shortcuts, as I don't have too many items I need to insert.

FastKeys has far more features than I need or will ever use, like a custom Start Menu, Auto Complete, Gestures, and options to launch applications and do complex text substitution.

I may eventually use some of those fancier features, but for now, I'm just happy that I'm able to automatically insert comment lines into my code in Visual Studio.

Like I mentioned, there are a ton of apps like this, so regardless of which one you choose, I would recommend that you try using one to save time typing.

May your coding be fast and efficient.


You can also find him on Google+ and Twitter



No comments:

Post a Comment