DEV Community

Scott Hintze for DealerOn Dev

Posted on

Supercharge Your Coding in Visual Studio

Keyboard Shortcuts

Hold Down Alt when Selecting Text

Let’s say you have a list of variables and you decide to change the scope from public to protected. You could use find and replace on the selected text, or you could just copy/paste over each modifier. Another option is block selecting. Holding down the Alt key while selecting allows you to include multiple lines to the position of the cursor rather than the entire line. The result is that when you type, cut or paste, it applies to every line selected. You could select all the public modifiers and simply type or paste protected to edit them all at once.
alt text

Ctrl + X and Ctrl + V with nothing selected

Cut and paste? What is so special about these? In Visual Studio. If you have nothing selected, the cut command will cut the entire line and paste will insert the entire line above the cursor. When moving lines around, this method is very fast.

Ctrl + K + C (comment) and Ctrl + K + U (uncomment) with nothing selected

These commands will comment and uncomment the entire line when no text is selected. When text is selected, they will comment and uncomment the selection. The uncomment shortcut will uncomment even if no text is selected as long as the cursor is within commented code.

Better Commenting

As nice as the default comment shortcuts are, you still have to use both hands. That can get tedious if you are doing a lot. Here is an easier combo to make it even faster.
Go to Tools>Options, then find the Environment>Keyboard node.
alt text

In the “Show commands containing box” enter “comment” (no quotes).
alt text

Select the Edit.ToggleLineComment shortcut and click in the “Press shortcut key” input field.
alt text

Press Left Ctrl + Left Windows, then click “Assign”
alt text

Repeat the process for Edit.UncommentSelection using Left Alt + Left Windows

Setting these shortcuts allows you to keep one hand on the mouse or arrow keys to navigate the code and the other to comment and uncomment quickly.

Visual Studio has a Back Button?!

Ever feel you are a bit like Alice and falling down the rabbit hole when navigating code? Visual Studio has a back button that tracks a history of where your cursor has been. Just click to go back through and retrace your steps. You can also click the down arrow to see the history list.
alt text

Tedious Code

Repetitive code, such as mapping database tables to object models, can be a real downer to a good programming groove. Here’s how to use simple Excel formulas to write hundreds of lines of code in seconds.
Get a list of the variable names or database fields without any extra punctuation. Then, paste the list into column A of your blank Excel sheet.
Excel string formulas are fairly straightforward. The basic syntax is =”string literal” & A1 & “string literal”. The quotes indicate literal text. Use ”” to escape a double quote in the string. Let’s say your variable name is Id and is located in the A1 cell of your sheet and you want to create a public string accessor property for it. The formula would be =”public string ” & TRIM(A1) & “{ get; set; }” Now, you just use the Excel fill handle and pull it down your list to "write" that line of code for each variable.
alt text
alt text

Discussion (1)

gravisus profile image
Andy Shevchenko

You don't need Excel for the last example, just use multiline cursors, or