Wednesday 3 March 2010

Enhancing the Visual Studio experience with some shortcuts you probably don't know

It's well known to most developers that Visual Studio has a whole plethora of shortcuts available. You can find most of these just by using your search engine.

While there are literally hundreds, and I strongly suggest taking the time to find them, I'll show you the three I use most, and then show you some shortcuts you probably won't know, even if you are a veteran of Visual Studio.

The shortcuts I use most (and probably already know) are:

CTRL + .
If you type something that requires a Using or Import statement, it will get a small red underscore beneath it. You can hover over it with the mouse (which is quite tricky because the cursor is a bar | shape and the underline is a flat _ shape) and view a little menu of suggestions to fix it, for example "using System.Drawing;" which when clicked will fill in the reference for you at the top of the page. It also shows you the shortcut to bring this menu up, which is SHIFT + ALT + F10.

What it neglects to tell you is that CTRL + . works just as well. So while you're typing, you can hit CTRL + . followed by ENTER to add the required 'using' or 'Import' statements automatically without interrupting your typing.

CTRL + K, C
CTRL + K, U

These are key chords, which are shortcuts that require two keypresses. To comment or uncomment some code, highlight it, then press CTRL+K. Keep the CTRL key held, and you will see "(Ctrl+K) was pressed. Waiting for second key of chord..." displayed at the bottom of Visual Studio. Keeping CTRL held down, press either U to Uncomment, or C to Comment your code.

TAB
Tab will tab your code to the left and right, but this also works if you select multiple lines of code to indent (and with SHIFT + TAB, un-indent). Very few developers know this, because we all imagine pressing tab with selected text will overwrite it.

The shortcuts you probably don't know

There are lots of global shortcuts that work in all applications, particularly Microsoft ones such as the Office suite. A lot of them are applicable to Visual Studio as well, but many have been lost over time and are very little-known. They won't be on pages showing you Visual Studio shortcuts because technically they are not VS specific, but they are absolutely applicable and your productivity would increase massively if you take the time to learn them.

The more spectacular ones are at the end.

CTRL + INSERT
SHIFT + INSERT

These are an alternative to CTRL + C and CTRL + V. Anyone that used Microsoft Quick BASIC will know that this was the only way to copy and paste in the IDE, but it's a universal shortcut throughout windows. Now you can copy and paste with the right hand as well as the left, which is massively advantageous as all of the navigation keys you need to whizz through your document are operated by the right hand - the INSERT, HOME, PAGE UP, DELETE, END, PAGE DOWN keys, and the arrow keys.

SHIFT
While simple, the Shift key is most powerful when combined with the other shortcuts below, so I have listed it first. Shift is used for selecting an area of text, most commonly by pressing SHIFT + Arrow keys. It's important to note that it can be used with combinations of other keys. Pressing SHIFT + END and SHIFT + HOME is incredibly useful yet so often overlooked.
Another trick is when you have selected a large block of text with the mouse and released the mouse button, you can press and hold SHIFT and continue to manipulate the selection. This is very useful if you select a large block of text with the mouse, then decide you want to deselect a few characters from the end, or if the selection includes a carriage return.

CTRL + HOME
CTRL + END

These shortcuts position the cursor at the start or end of a document. Remember, you can use them with SHIFT key.

CTRL + PAGE UP
CTRL + PAGE DOWN

These shortcuts move the cursor to the top or bottom of the window, without scrolling your document up or down. They work with SHIFT.

CTRL + UP
CTRL + DOWN

These shortcuts are specific to Visual Studio, but allow you to scroll up and down the page without the cursor moving.

CTRL + LEFT
CTRL + RIGHT

These are probably the most underused and yet most powerful shortcuts. They move the cursor one word to the right or left. It took me a few days of forcing myself to use these shortcuts until they became second nature, and the payoff is considerable. Combined with SHIFT, you can now manipulate code considerably faster than holding an arrow key and watching the cursor crawl along.

CTRL + BACKSPACE
CTRL + DEL

This works in some editors and Visual Studio, and works as above but deletes one word before or after the cursor.

CTRL + -
This is a Visual Studio shortcut to navigate back, and it has a corresponding button in the toolbar by default. The chances are, using all of these shortcuts at high speed, at some point or another you will accidentally end up in the wrong place, usually by pressing CTRL+HOME or CTRL+END. Simple press CTRL + - and the cursor will magically jump back to where it was last, so you can continue.



It's important to remember there are SHIFT and CTRL keys on both sides of the keyboard. This makes it considerably easier to press the shortcuts, especially when doing something tricky such as CTRL + SHIFT + Right Arrow to select the words one by one immediately to the right of the cursor. You can press Left CTRL, Right SHIFT, and the arrow key without any kind of unnatural movement of the hands.

Try these shortcuts and stick with them for a few days, and you'll be zipping around Visual Studio faster than ever before!

No comments:

Post a Comment