Wednesday, February 25, 2015

Useful Visual Studio shortcuts, tips and tricks from the trenches

Visual Studio is a beast of an IDE with plenty of gems of functionality hidden away possibly never to be found in your day-to-day work. Below are some handy shortcuts which I use and tips / tricks I have picked up along the way to help get the most out of using Visual Studio.

Please note, not all of these will work in versions of Visual Studio prior to Visual Studio 2013.

Keyboard Shortcuts:
Ctrl + [ + S - Highlights the file you are viewing in the Solution Explorer. 
Alternatively type “track” into the Quick launch (Ctrl + Q) bar and you can enable a setting that will automatically track the file you are viewing in the Solution Explorer.

Ctrl + K + C – Comments the selected text.

Ctrl + K + U – Un-comments the selected text.

Alt + [UP] – Moves the highlighted text up a line (great for refactoring and moving code around).

Alt + [DOWN] – Moves the highlighted text down a line.

Alt + [LEFT] – Displays the Intellisense selector.

Shift + Delete – Deletes the current line (handy when clearing out unnecessary white space).

Ctrl + Alt + L – Moves the focus to the Solution Explorer (Allows you to navigate around the solution explorer without having to use the mouse).

Ctrl + G – Go to line number.

(Ctrl + R) + (Ctrl + R) or F2 – Rename (Very useful when renaming methods / variables).

Ctrl R, M – Extract Method.

(Cursor within Implemented Interface declaration) Ctrl + . – Displays the implement interface context menu.

(Cursor within out of scope item due to missing using statement) Ctrl + .  – Displays the using statement import / generate class context menu.

(Cursor is beside a brace { or }) Ctrl + ] – Toggles between opening and closing braces (Use in conjunction with the shift key to highlight everything within a particular braced scope.

(Cursor is in the middle of a line) Ctrl + Enter – New line above the current line.

(Cursor is in the middle of a line) Ctrl + Shift + Enter – New line below the current line.

Ctrl + , - Navigate to a file. Type the filename in and Visual Studio will locate file names which match what you are typing. Easy way to locate files in a project. You can use the pascal casing trick demonstrated in Intellisense – Pascal Casing below.

Alt + F12 – Peek. The equivalent of Go to Definition (F12) but within your current tab.
(Whilst in peek window) Ctrl Alt – and Ctrl Alt + - Cycle through peek tabs.

Display all thrown exceptions (handy for locating swallowed exceptions):
By default, Visual Studio will only prompt when an exception is “User-unhandled”. Sometimes it is necessary to be alerted whenever an exception is thrown regardless if it’s being handled or not (just in case it’s being swallowed or you are interested in the specific location that the exception is being thrown from).

The Exceptions (Debug > Exceptions… or Ctrl + D, E) dialog can help here.

Specify which exceptions you want to be notified of whenever an exception is thrown by checking the corresponding “Thrown” check box and you are ready to go.

Scroll bar – Map Mode:
If you have a wide screen monitor and want to maximize the use of that white space real estate usually located in the centre of the screen, try enabling map mode for the vertical scroll bar.
Right click on the vertical scroll bar and select “Scroll Bar Options…”. In the Behaviors section, select the “Use map mode for vertical scroll bar” radio button and configure if you would like a preview tool tip plus the width of source overview.

When configured you will have a nice source map with optional preview tool tip at your disposal.

Intellisense – Pascal Casing:
You do not need to type the full name for Intellisense to find the item you are after. For example, for Console.WriteLine, Console.WL is all you need.

Special thanks go out to Tarek Madkour for his Tech Ed talk on Visual Studio IDE Tips and Tricks as a number of these came from there.