My favourite Visual Studio tips and tricks

François shares 17 tips and tricks to be efficient in Visual Studio.
Dev's Corner
Spiria's Dev Team
2019-12-02 11:43
5 minute read
<div><p>Hi everyone! Today, I thought I’d share my best time-saving tips for Visual Studio.</p><p>Just so you know, I usually work in C++ and C#, but most tips should apply to other languages as well.</p><p>Here we go!</p><h2>Editing tips</h2><p>1. <b>Go to All: Ctrl + ,</b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/01_go_to_all.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/01_go_to_all.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/01_go_to_all.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/01_go_to_all.png" style="width: 452px; border-style:solid; border-width:1px;" alt="Go to All" title="Go to All"></source></source></source></picture></p><p>You can quickly find anything and everything (i.e., files, classes, etc.) in your project with this tool.</p><p>2. <b>Go to the previous edit location: Ctrl + -</b></p><p>3. <b>Go to the next edit location: Ctrl + Shift + -</b></p><p>4. <b>Go to the beginning or end of a code block: Ctrl + [ et Ctrl + ]</b></p><p>5. <b>Go to definition: F12</b></p><p>A basic but critical command to quickly navigate to class and method definitions.</p><p>6. <b>Comment out the selected block of code: Ctrl + K + C</b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/02_comment_block.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/02_comment_block.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/02_comment_block.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/02_comment_block.png" style="width: 224px; border-style:solid; border-width:1px;" alt="Comment out the selected block of code" title="Comment out the selected block of code"></source></source></source></picture></p><p>7. <b>Uncomment the selected block of code: Ctrl + K + U</b></p><p>8. <b>Autoformat the selected code: Ctrl + K + F</b></p><p>This automatically fixes whitespace and code indentation according to your settings.</p><p>9. <b>Vertical text box selection: Alt + Mouse click</b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/03_alt_box_select.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/03_alt_box_select.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/03_alt_box_select.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/03_alt_box_select.webp" style="width: 213px; border-style:solid; border-width:1px;" alt="Vertical text box-selection" title="Vertical text box-selection"></source></source></source></picture></p><p>This is very useful to remove the first characters of text on multiple lines of code.</p><p>10. <b>Toggle between a header and its source file (C++): Ctrl + K + O</b></p><p>11. <b>Rename a variable: Ctrl + R + R</b></p><p>This is much faster than manually finding every occurrence of the variable on your own.</p><p>12. <b>View whitespace: Ctrl + R + W</b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/04_whitespace.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/04_whitespace.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/04_whitespace.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/04_whitespace.png" style="width: 195px; border-style:solid; border-width:1px;" alt="View whitespace" title="View whitespace"></source></source></source></picture></p><p>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.</p><p>13. <b>Code snippets<i> (snippets)</i></b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/05_code_snippets.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/05_code_snippets.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/05_code_snippets.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/05_code_snippets.png" style="width: 611px; border-style:solid; border-width:1px;" alt="Code snippets" title="Code snippets"></source></source></source></picture></p><p>If you find yourself repeatedly writing the same instruction, consider using code snippets.</p><p>Here’s an example to get you started: I often work in Release mode to speed things up in very large client projects.</p><p>When I need to debug a method more thoroughly, I can quickly disable optimizations by typing the following shortcut: P + O + Tab.</p><p>Before, I had to repeatedly write: <code>#pragma optimize("", off)</code>.</p><p>To achieve this shortcut, I just had to add a keyboard shortcut (Tools -&gt; Options -&gt; Environment -&gt; Keyboard) to the following code snippet:</p><pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;&lt;CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt; &lt;CodeSnippet Format="1.0.0"&gt; &lt;Header&gt; &lt;Title&gt;Pragma Optimize Off&lt;/Title&gt; &lt;Shortcut&gt;po&lt;/Shortcut&gt; &lt;Description&gt;Code snippet to disable optimizations&lt;/Description&gt; &lt;Author&gt;Me&lt;/Author&gt; &lt;SnippetTypes&gt; &lt;SnippetType&gt;Expansion&lt;/SnippetType&gt; &lt;/SnippetTypes&gt; &lt;/Header&gt; &lt;Snippet&gt; &lt;Code Language="cpp"&gt;#pragma optimize("", off)&lt;/Code&gt; &lt;/Snippet&gt; &lt;/CodeSnippet&gt;&lt;/CodeSnippets&gt;</code></pre><h2>Debugging Tips</h2><p>14. <b>Breakpoint Actions</b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/06_breakpoint_action.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/06_breakpoint_action.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/06_breakpoint_action.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/06_breakpoint_action.png" style="width: 100%; border-style:solid; border-width:1px;" alt="Breakpoint Action" title="Breakpoint Action"></source></source></source></picture></p><p>Use Breakpoint Actions instead of <code>printf</code> and <code>cout</code>. 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&gt;</p><p>15. <b>Conditional Breakpoint</b></p><p>A basic and very useful debugging tool, it is right next to the Actions in the breakpoint settings options pane.</p><p>16. <b>Run to cursor: “Run execution to here.” No breakpoint needed!</b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/07_run_execution_to_here.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/07_run_execution_to_here.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/07_run_execution_to_here.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/07_run_execution_to_here.png" style="width: 231px; border-style:solid; border-width:1px;" alt="Run to cursor" title="Run to cursor"></source></source></source></picture></p><p>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.</p><p>17. <b>Drag the instruction pointer</b></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/08_instruction_pointer.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/08_instruction_pointer.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/4218/08_instruction_pointer.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/4218/08_instruction_pointer.png" style="width: 100%; border-style:solid; border-width:1px;" alt="Drag the instruction pointer" title="Drag the instruction pointer"></source></source></source></picture></p><p>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.</p><h2>Conclusion</h2><p>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!</p><p>Until next time!</p></div>

Want to Work Together?

Every great project starts with a conversation.