Friday, August 10, 2007

Visual Studio Shortcuts

Visual Studio Shortcuts Here are some of the Visual Studio shortcuts I use.

Commands

Visual Studio includes a basic command-line prompt that is very useful for opening files. One way to enter commands is to press control+/, and then type >. You can then start typing a command, like "open", and then part of a filename. The nice part is that this autocompletes, so this is a great way to open any file in your solution quickly. Press enter and the command is run. All of the commands you see in Tools-Options-Keyboard can be entered too, such as "File.CloseSolution" and so on.
There is a "command window" that is dedicated to this. I've mapped Ctrl+Space to View.CommandWindow, which is quicker than Ctrl+/, >. You can set up an alias to any command. For example, to set up an alias to a commonly used file, enter this:
alias m File.OpenFile CommonlyUsedFile.cs
Now, you can press Ctrl+Space,m,Enter to quickly open that file - very useful if you have a large solution. There are many built-in aliases such as "open" for "File.OpenFile". View this list by typing "alias" with no parameters, or remove an alias with the syntax "alias m /delete". I deleted most of the other aliases, as I'd rather type Ctrl+F4 then "close", and so on.
Useful aliases:
sh Tools.Shell
o File.OpenFile
cls Edit.ClearAll
g Debug.Start
bl Debug.Breakpoints
kb Debug.ListCallStack

Macros

Macros are a great idea, but I'm not too impressed with them in VS. VB is such an ugly language, and there is a distracting pause when running a macro. Anyways, I'd thought I'd try my hand. Here's something that will complete all nested parens and end the line. For example, if you type Console.WriteLine(a.ToString( and run the macro, it will add "));" and start the next line. I've mapped this to Control+Enter, and it can be useful if you are too lazy to count how many parens to close.
Sub CompleteLine()
Dim line As String
DTE.ActiveDocument.Selection.StartOfLine(0)
DTE.ActiveDocument.Selection.EndOfLine(True)
line = DTE.ActiveDocument.Selection.Text

Dim nparens As Integer, i As Long
nparens = 0
For i = 1 To Len(line) - 1 Step 1
    If StrComp("(", Mid$(line, i, 1), vbTextCompare) = 0 Then
  nparens = nparens + 1
    ElseIf StrComp(")", Mid$(line, i, 1), vbTextCompare) = 0 Then
  nparens = nparens - 1
    End If
Next

Dim strResult As String
strResult = ""
For i = 1 To nparens Step 1
    strResult &= ")"
Next
strResult &= ";"

DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.Text = strResult.ToString()
DTE.ActiveDocument.Selection.NewLine()
End Sub
I also have a macro that duplicates the current line, but I'm frustrated by the pause. My wish is to come up with something that could organize my Ctrl-Tab into visual, and not MRU order, but that's a story for another day.

Snippits

Snippits are a great feature of VS 2005. You can easily create your own snippits by creating xml files. There is a nice GUI for creating Snippits called Snippy (A Visual Studio 2005 PowerToy), but it seems to have dissapeared entirely along with gotdotnet.com. So, I've posted it here (378k).
I wrote snippits for creating unit tests, switch statements, and some XAML structures. Most often I used prop and propd for quickly creating properties.

Shortcuts

Finally, here are some keyboard bindings that I find useful:
Ctrl+Space View.CommandWindow
Ctrl+Q Edit.CommentSelection
Ctrl+Shift+Q Edit.UncommentSelection
Ctrl+Alt+, Toggle Bookmark
Ctrl+, Next Bookmark
Ctrl+Shift+, Prev Bookmark
Ctrl+Alt+W Watch Window
Ctrl+Alt+K Call stack
Ctrl+Alt+R Toggle Word wrap
Ctrl+Shift+Alt+W File.CloseSolution
If you happen to have MbUnit (which is cool) or VisualSVN,
Ctrl+T MbUnit – Run Tests
Ctrl+Shift+R VisualSVN-Revert Changes current item
Ctrl+Shift+H VisualSVN- Show current file changes
Ctrl+Shift+Alt+H VisualSVN – Show All Changes

Some of these override standard key bindings, but it’s easy to assign another. I have Ctrl+Shift+Space to be Edit.CompleteWord rather than Ctrl+Space. Anyways, those are some ways to make Visual Studio work for you. The Clipboard ring is nice also.

No comments: