I have been a developer now for over 15 years and in that time Visual Studio has undergone some tremendous changes. New features are constantly being added and sometimes little things that make your life easier go unnoticed until you see someone else use them. Then you wonder how long you have been mugging yourself off by not using said feature.
Here are a few of the best features that members of our team thought were game changers when they discovered their existence.
1. Go To (Control+Comma) VS2017
If you are working in a project with a lot of files it can quickly become a pain to get to the file you want. You can use Ctrl+, or Ctrl+t to open a file search dialog which you can use to quickly find a file, class, method, property etc.
You can find out more about this feature and related Go To features in this blog post from the C++ team
2. Step Into Specific VS2010
If you have ever hit a breakpoint on a method call where many of the parameters are themselves other method calls, you may have felt the pain of having to step into and step out of each method until you get to the one you want to debug. The step into specific feature is there to help you out in this scenario. Right click the current line, then click step into specific, then click the method you want to step into!
3. Game changing keyboard shortcuts
Some pretty basic keyboard shortcuts were mentioned highly by our team, so to keep this post short, I will consolidate most of them here.
F12
When you click on a class or method, then press the F12 key, you are taken to the definition of what is under the cursor. This is the same as Right Click -> Go To Definition. When pair programming with one of our apprentices I showed them this and it blew their mind.
Shift+Delete
This cuts the current line. I use this one quite often to remove blank lines which that have various amounts of leading whitespace.
Ctrl+C (with nothing selected)
I have seen people pressing home, then shift+end to select the entire line, then ctrl+c to copy it. This can all just be done by clicking anywhere on the line and pressing ctrl+c save those key presses people!
Ctrl+KC and Ctrl+KU
Another one from pair programming, watching someone manually comment out multiple lines is painful. Pressing Ctrl+KC will comment out the entire selection. Then pressing Ctrl+KU will uncomment the selection.
Ctrl+Enter
I only found out about this one myself after speaking to my team about writing this blog post. This will create a new line above the current line and move the cursor to it.
4. Alt Select VS2010
If you hold Alt while dragging over a selection you will get box mode selection. This is useful if you want top just copy a load of property names from a mapping function for example.
To find out more about box selection you can check out this blog post from the Visual Studio team.
5. Ctrl Drag
If you’re a fan of using your mouse for selection rather than the keyboard, then this tip is for you. Most of the time you want to select entire words, so instead of clicking the start of your selection, try Ctrl+clicking the first word of your selection. Then while still holding Ctrl, drag to the last word in your selection. This way you don’t need to be as accurate with your horizontal mouse movements.
6. Code Snippets
You may have noticed in your intellisense that there are some entries which look like they are missing an icon. These are code snippets. If you press tab with a code snippet selected in your intellisence context menu, Visual Studio will inset a block of code with some highlighted sections. You will have the first highlighted section selected by default for you type in your first variable. You can then press tab to highlight the next variable. Finally press enter to complete the code snippet and move your caret to the end of the snippet.
My most commonly used snippet by far is the prop snippet. See it in action below. I type prop, press tab, type the property type, press tab, type the property name, press enter, rinse and repeat.
7. Surround (Ctrl+K+S)
When you are writing code you don’t generally write code from top to bottom first time. It is an iterative process and you generally start in the middle of what you are trying to do. The surround feature lets you wrap the selected text in many useful constructs like if statements, for/foreach loops, try/catch statements and many more. I use this for try/catch statements all the time and it saves a lot of typing. Simply select what you want to surround, press Ctrl+K+S and start typing what you want to surround with, then press tab or enter.
8. Format Document
Have you ever copy pasted code from a sample and got some whack indentation? Simply press Ctrl+K+D and Visual Studio will format the entire document for you. This works for all kinds of documents, not just .cs files. Be careful when formatting razor templates, the result is often harder to read than before it was formatted.
9. Conditional Breakpoints
This has been around seemingly forever but if you are new to coding you might not know you can do this. If you have some code which contains loops and you are only getting an error on say the 42nd itteration, then a newbie may put in a breakpoint and press F5 41 times until they get to the iteration they want to expect. While this does work, it’s not the most effective use if your time. Instead, you should use a conditional breakpoint! Right click your breakpoint and click conditions. Now you can type an expression which will cause the breakpoint to trigger when it evaluates to true. Conditional breakpoints destroy performance so don’t expect your code to run fast while you have them enabled, even if they never trigger. Remember to turn them off when you are done.
10. Disable/Enable All Breakpoints
Have you ever been deep into a debugging session and a colleague walks past your desk and wants to see what you are working on. So you start the project only to have to F5 your way past a million breakpoints before you can show them anything? Well this tip is for you! You can temporarily disable all breakpoints by clicking the debug menu, then clicking “Disable All Breakpoints”. This is useful because it keeps your breakpoints where they are, so once you have shown your colleague your work, you can enable them all again and continue debugging where you left off.
11. Bonus tip – all the keyboard shortcuts!
A kind Redditor put us on to this website. No further explanation required given it’s name is so self explanatory:
There are loads of cool shortcuts or neat features that we could list here, so this may become a series of posts with more tips in the future.
If you have any tips of your own or stories of face palm moments when you discovered a feature that would have made your life so much easier had you known about it sooner, please let us know!