Visual Studio Tips & Tricks

Now I'm going to show you all the tricks I know when it comes to my favorite programming IDE, Visual Studio 2017. I hope you learn a trick or two.

Visual Studio Tips & Tricks

Now I'm going to show you all the tricks I know when it comes to my favorite programming IDE, Visual Studio 2017. I hope you learn a trick or two.

This is all tested on my current install of Visual Studio 2017 version 15.8.2.

The keyboard shortcuts

Clean up and sort using statements
To clean up and sort using statements use Ctrl + r + g.
ctrl_R_G

And if your coding rules want you to put System first you can set that option also
sortusing

Toggle through the clipboard
To toggle through the clipboard use Shift + Ctrl + v. This is one of my most used ones.
ctrl_shift_v

Add blank line after the line you are in
Hit Ctrl+Shift+Enter where ever in your line. It will add a blank line after yours.

Cut the line the cursor is on
Use Shift+Delete (or Ctrl + L) to basically cut (deletes) out the line where the cursor is. That way it is on the clipboard where you can get it again. This is much better than just highlighting the current line and deleting it.

Delete a line without copying it to the clipboard
Use Ctrl+Shift+L to delete a line without copying it to the clipboard.

Delete next word on the RIGHT
Delete word to the right of cursor with Ctrl + Delete

Delete next word on the LEFT
Delete word to left of cursor with Crtl + Backspace

Duplicate a line
Duplicate a line with Ctrl + e + v. I have mapped this to Ctrl+d. Why that is not the default I don't understand.
ctrl_e_v

Move a code line up/down
Move a code line up/down with Alt + Up/Down
alt_up_down

Full screen
Put your current open file in full screen mode with Shift+Alt+Enter. This comes handy when there is lot of code to look at.

Navigate to top/bottom
Navigate to the top or bottom of the current document you are in with Ctrl + Home/End.

Format document
Format document to code formatting settings with Ctrl + k + d. No need to use space/delete to format curly braces and indentation.

Comment and un-comment code
Comment and un-comment code with Ctrl + k + c and ctrl + k + u.

Open and close methods and regions
Open and close methods and regions with Ctrl + m + o/p . You can also try Ctr + M + H for current selection.
ctrl_m_op

Navigate to previous file
Navigate to previous file with Ctrl + _ (or Ctrl + Þ on Icelandic keyboard). Did you debug beyond someplace you want to go back to? Use this one.

Navigate forward
After navigating back to previous file you can go back with Ctrl + Shift + _ (or Ctrl + Shift + Þ on Icelandic keyboard).

Go to the code you changed last
It can be very handy to be able to back to the code you where just changing. Especially after debugging somewhere deep. Try Shift + Ctrl + Backspace

Show method signature
Need to know the method signature of a method you are using? You can either delete a comma and then re-enter it and get the signature OR you can use Ctrl + Shift + Space

Copy part of many lines
To copy/cut and pasting down rows of data Alt+ mouse drag comes in handy. Here I just pressed the Alt key and dragged the mouse down and then hit 6 on the keyboard.
alt_mouse_drag

Multi-Caret support
When Alt+ mouse drag doesn't do the trick you can just Ctrl + Alt + Click around and then start typing.
MultiCarets

Search everywhere
Best way to search everywhere for everyting is to use Ctrl + ,. You'll get a good overview of the "things" you find.

Replace selected text
Replace selected text with Ctrl + h. Try highlighting some code and hit Ctrl+h and you get the option to replace it with something else. And it does not use your clipboard so the what you have there is ready to be used with Ctrl+v. Note that you can then use Alt+a to replace all.

Go to line
Go to line number in current document with Ctrl + g. This comes in handy when you have a stack trace with the number of the line where the code broke and the class is long.

Show method signature
Need to see of what type the parameters in a method are? Ctrl + Shift + Space display the currently used overload.

Pending change filter
Pending change filter for the Solution Explorer is activated with Ctrl +- p. If you just want to see the files you have changed (and not just in the pending changes window) this is for you.

Add new class
Add new class the current place (folder/project) with Shift + Alt + c.

Navigate to method implementation
Navigate to method by clicking on it with Ctrl down (Ctrl + mouse click). This used to be an addon. Good add VS!

Go to the implementation
Go to the implementation with Ctrl + F12. Useful if you have an interface method declaration you need to find the implementation for.

Find all references
Find all references of a selected element with Shift + F12.

Go to the implementation
Go to the implementation* of a selected element e.g on a method with F12.

Rename a variable
Have your caret cursor on the variable and hit Ctrl + R R

Stop debugging
Easiest way is to use Shift + F5

Active windows and files window
Get to active windows and files with Ctrl + Tab.
Ctrl_tab_active_files

Other VS windows

Bookmark window
Use Ctrl + w, b to open it up but use Ctrl + k + k to create a bookmark in your code.
You can then order your bookmarks as you see fit.
Bookmark-window

Conditional brakepoints
If you need to debug e.g a loop of 1 million items and you need to know what item number 500.000 is doing, you need this one.
conditional_brakepoints

Track active item in Solution Explorer
This is a must setting. If you are navigating through your code (by Ctrl+mouse click or F12) files get opened in tabs. With this setting you will see where they live in the solution structure.
trackactive

Breake on ALL exceptions
If you have a large codebase and you don't want to only stop in try/catch's you can go to Exception Settings and select "Common LanguageRuntime Exceptions"Brake_on_exception

Force Visual Studio to performe garbage collection on itself
Hit Ctrl+Shift+Alt+F12 twice! Ask Mads Kristensen on Twitter more detail.

Want more?
Got to #vstips on Twitter. There are few more there

Addons

There are all kinds of addons for Visual Studio on the marketplace. Here are few that I use.

Spellchecker
This one is a must for me (none-english-native speaker). It even checks spelling in the commit window. Give Visual Studio Spell Checker a spin.

Roslynator
A collection of 190+ analyzers and 190+ refactorings for C#, powered by Roslyn.

Clean Solution
Cleans up the solution for the next time you open it. Closes open documents and collapses folders automatically when a solution is being closed. Try it out.

Open Command Line
The easiest way to open a command prompt from Visual Studio.
context-menu

Live Share
Share your debugging session with your collegueas with this Live Share addon). It´s an addon atm but will be moved into Visual Studio when its out of preview.

IntelliCode
IntelliCode is a set of AI-assisted capabilities that improve developer productivity with features like contextual IntelliSense, inference and enforcement for code styles, and focused reviews for your pull requests (PRs.) Get it here.

Tip of the Day provider for Visual Studio
And then to learn more every day you should add Hot Tips to discover new features and shortcuts through pop-up windows on soulution load.

Git pull keyboard shortcut
Assign a keyboard shortcut to perform a “git pull” so you don’t have to use CLI or Team Explorer to ensure your repo is up to date. (Supported in VS2019)

git-pull

Misc

Go to cmd from Explorer
This is not an addon but I use it a lot and people don't seem to be aware of it. It's a neat trick to go to the command line straight from the Explorer window. Just type in cmd and press enter.
cmd

I hope you learned something to day. If you want to learn more you can check out various different shortcuts and tricks in places like.

p.s
I used ScreenToGif to create all the gifs in this post. I totally recomend it.

e.s
The main image for the blog post was create on my IPhone using Adobe Spark because there I don´t have to search the internet for free images, I just mix and match what I can find in Spark. I will probably create lots of strange images from now on :-)