My favourite Visual Studio tips and tricks

François shares 17 tips and tricks to be efficient in Visual Studio.

Hi everyone! Today, I thought I’d share my best time-saving tips for Visual Studio.

Just so you know, I usually work in C++ and C#, but most tips should apply to other languages as well.

Here we go!

Editing tips

1. Go to All: Ctrl + ,

Go to All

You can quickly find anything and everything (i.e., files, classes, etc.) in your project with this tool.

2. Go to the previous edit location: Ctrl + -

3. Go to the next edit location: Ctrl + Shift + -

4. Go to the beginning or end of a code block: Ctrl + [ et Ctrl + ]

5. Go to definition: F12

A basic but critical command to quickly navigate to class and method definitions.

6. Comment out the selected block of code: Ctrl + K + C

Comment out the selected block of code

7. Uncomment the selected block of code: Ctrl + K + U

8. Autoformat the selected code: Ctrl + K + F

This automatically fixes whitespace and code indentation according to your settings.

9. Vertical text box selection: Alt + Mouse click

Vertical text box-selection

This is very useful to remove the first characters of text on multiple lines of code.

10. Toggle between a header and its source file (C++): Ctrl + K + O

11. Rename a variable: Ctrl + R + R

This is much faster than manually finding every occurrence of the variable on your own.

12. View whitespace: Ctrl + R + W

View whitespace

This may just be my OCD, but I love seeing the exact whitespace in a document. I don’t like having to guess whether it’s a tab or a space.

13. Code snippets (snippets)

Code snippets

If you find yourself repeatedly writing the same instruction, consider using code snippets.

Here’s an example to get you started: I often work in Release mode to speed things up in very large client projects.

When I need to debug a method more thoroughly, I can quickly disable optimizations by typing the following shortcut: P + O + Tab.

Before, I had to repeatedly write: #pragma optimize("", off).

To achieve this shortcut, I just had to add a keyboard shortcut (Tools -> Options -> Environment -> Keyboard) to the following code snippet:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
      <Header>
         <Title>Pragma Optimize Off</Title>
         <Shortcut>po</Shortcut>
         <Description>Code snippet to disable optimizations</Description>
         <Author>Me</Author>
         <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
         </SnippetTypes>
      </Header>
      <Snippet>
         <Code Language="cpp">#pragma optimize("", off)</Code>
      </Snippet>
   </CodeSnippet>
</CodeSnippets>

Debugging Tips

14. Breakpoint Actions

Breakpoint Action

Use Breakpoint Actions instead of printf and cout. I love Actions: they allow you to quickly adjust your temporary logs without recompiling each time, and having to remember to clean them up when they are no longer needed./p>

15. Conditional Breakpoint

A basic and very useful debugging tool, it is right next to the Actions in the breakpoint settings options pane.

16. Run to cursor: “Run execution to here.” No breakpoint needed!

Run to cursor

I know some people find this widget annoying, but it can be more than just a mouse pointer trap. Try pressing the little green Play button to quickly skip to that point in the code while debugging. Thanks to this tool, you no longer need to add a temporary breakpoint to stop at the desired point.

17. Drag the instruction pointer

Drag the instruction pointer

Yep, you can move the yellow arrow while you’re debugging. It’s very useful to be able to move it up in the code you are debugging to repeat certain code statements that are being executed. You can also easily skip certain code blocks while debugging by dragging it lower down in the code. The beauty here is that you can do this without restarting the application each time.

Conclusion

I hope you discovered a trick or two that will speed up your daily coding workflow. Please add your own favourites in the comments area below!

Until next time!

Discover our
success stories.

A software development project?
Contact us!

This entry was posted in Desktop application development
by François Mayrand.
Share this article