This article is a part of series of articles written on Visual Studio Tips & Tricks and this is the second article.
In last article, I have explained 16 tips. So now, I am continuing from number 17.
- Box Selection or Vertical selection
In Visual studio 2010 and onward, we can do box selection, which means, selecting your code vertically -- it provides a lot of benefits. e.g. - Select your code and change some words, like modifying access modifier, removing extra characters formatting etc. in multiple lines simultaneously.
If you are thinking that your code is not aligned properly and you are not able to do the box selection, in that case, you can align your code very easily using the Visual Studio Extension "Align Assignment" or “Code Alignment”. These are really good and helpful tools for formatting. I'll explain about it later in this article.
Vertical selection is not just limited to your VS code. You can also format your external code using this. Suppose, I am copying some code from an online website and it is showing me a line number on each line; and I am not able to compile the code properly. OK, no issues. We can handle it using the box selection very easily.
Select the initial 2-3 characters of each line and just press the delete button to delete all the selected text. Now, you'll find that line numbers have been removed from all the lines and you are having a neat and clean code. Just press F6 to build it.
Shortcuts:
For box selection, we use the shortcut “Shift +Alt + Arrow”. Left Arrow, Right Arrow, Up Arrow & Down Arrow can be used to move the cursor left, right, up & down respectively.
Or
Shift + Mouse,
Change Case Shortcuts: Ctrl + U & Ctrl + Shift + U,
This feature is one of the best features of any editor, and I use this feature on daily basis. Given below is the list of a few editors which support box selection:
Visual Studio Code Editor - a cross platform Code editor that supports Windows, Mac and Linux.
Notepad++ & Sublime text editor also support box selection.
- Delete a line
You can delete a line by selecting it and pressing the delete key. But, there are three smarter ways to delete a line.
- Shift + Delete: It will delete the current line. To delete the entire line, just keep the cursor anywhere on line and press Shift + Delete.
note: If you select some words of a line and then press “Shift + Delete”, then only selected words will be delete. So, if you want to delete the entire line, please make sure you haven’t selected any character.
- Ctrl+ L: It will delete the current line and copy the content to clipboard.
In case of Shift + Delete, if you want to delete the entire line, your cursor should be on same line but no character should be selected whereas in case of “Ctrl+ L”, it always deletes entire lines regardless if a character or word is selected or no character is selected.
- Ctrl + X: It will remove the selected text. If the entire line is selected, it will remove the entire line text but it still leaves a blank line.
So, there are 2 differences between Ctrl + L & Shift + Delete.
- “Ctrl + L” always deletes the entire line regardless of selected part of line.
- “Ctrl + L” deletes the text and copy it to clipboard. So, it behave like combination of “Ctrl + X” & Shift + Delete.
Most of the times, I prefer to use Ctrl + L.
- Navigate To/Quick Search
This feature is very useful to search any method property or type inside all the projects. In short, it is very simple and it is Ctrl +, (Ctrl + Comma).
It’s scrollable, draggable, and automatically moves the cursor to the selected item.
Search a matching item
It searches for any matching property/method/types etc. and displays all the matching names and automatically moves the cursor to the file where actual code is written.
It also selects items which are matching partially i.e. if the item is containing the searched text.
As you can see I have searched for Name but it is displaying Name, FirstName, MiddleName, LastName & NickName along with their type names.
Search Multiple Words
We can do search by separate words also, and it will search for all the matching items and move the cursor to the selected one. As you can see in the below screenshot, I have typed “First Name” and it searched all the items which contain “First” & “Name”.
Pascal Case Searching
In the below screenshot, I have typed “FN” and it searches all the items that matching Pascal case search criteria.
Searching a Type
So far, whatever I have searched, it is searching across the projects. If you want to make changes, you can do this. It provides some search options settings which you can set by clicking on dropdown. Refer to the below screenshot for more details.
- Use web essential for bundling, Minification and sprite images
Web Essential is one of the best Visual Studio extensions which is very popular and used by almost all the web developers. Using Web Essential extension, we can do bundling, minification and even sprite images. If you are not aware about bundling, minification and image sprite, you can visit the below article:
Resource Optimization (Bundling And Minification) Boost Your Website Performance
Apart from that, Web essential extension do a lot more,
- Create Color Palette
- Create Solution Settings
- Encode Selection
a. HTML Encode
b. HTML Attribute Encode
c. HTML Decode
d. URL Encode
e. URL Decode
f. JavaScript String Encode
- Transform Selection
a. To Title Case
b. Reverse
c. Remove Diacritics
d. MD5, SHA-1, SHA-256, SHA-384 & SHA-512
- Selected Lines
a. Sort selected lines ascending : Alt + 3
b. Sort selected lines descending: Alt + 4
c. Remove duplicate lines
d. Remove empty lines
- Use spell checker for improvement of comments.
We should install spell checker in Visual Studio because it helps a lot to correct the spelling of a misspelled word. For example, I have written a word “diakritikos” but it is non-English word. So, it is suggesting some correct English words, as you can see in the below screenshot.
But, it is not limited to only one word correction. If you would like to check spellings of all the comments (including normal comments and XML documentation comments) for entire solution, you can do it in one go.
There are many VS extensions available for spelling check and they provide different features. If you would like to spell check for an entire solution, you can download the extension “Visual Studio Spell Checker”. For reference, I have attached a screenshot of Extension and update window from where you can search and download it.
Once it is installed, go to the tools menu and select spell checker. You will find the following options inside it:
- Spell Check Current Document
- Spell Check Entire Solution
- Spell Check Current Project
- Spell Check Selected Items
- Open the solution/Project Spell Checker Windows
- Edit Global Configuration
- Make your pop-up boxes transparent
Your pop up box usually covers the background data and you are not able to view the code in the background. Sometimes, we have to close the pop up boxes and go back to the Visual Studio code in order to see what is written, But in fact, it is not required. You can see the code written in the background by just making your pop-up box transparent.
So normally, a pop-up box looks something like:
But, if I press Ctrl button, it will be transparent and all the data will be visible which is hidden behind this pop up box. It will look like the below screenshot:
- Use “new file” extension to add files quickly in visual studio.
If you would like to add files quickly, you can install “new file” extension which helps you to add file very quickly.
Just select the project or folder in which you want to add file and press the keyboard shortcut: “Shift +F2”.
This shortcut is a quick way to add a new file.
- Use TFS go offline feature instead of comparing it through beyond compare or any other tool.
Sometimes, it happens that we modify some file outside the Visual Studio editor and TFS is not able to detect those changes then we can use the “TFS go offline feature” feature to again go online to detect all those changes.
Suppose, we have mode code changes from outside visual studio editor and those changes are very large or restoring back from somewhere else which have hundreds of different files -- in that case it is very difficult to compare all those files manually or using any third party comparing tools.
If we use some comparing tools like beyond compare or any other tools we have to go through all the files which have different codes but there is no guaranty that TFS will detect all those changes.
So best option is to set TFS to “go offline” and then “go online”. It will detect all those changes.
For more details you can go through the below links:
Check-out Files in Visual Studio 2015 TFS Modified from Outside
- Always use conditional breakpoint if the code is hitting multiple times
If you are debugging your code and the particular section of your code hitting several times which is irritating you and wasting your time then you can use conditional breakpoint and set the condition, then debugger will only come at that breakpoint when the specified condition is met.
- Test Cases Tips
For complex code scenarios, always write test cases. You can use NUnit or any other tool for this.
Use code coverage tools instead of manually going through line by line and checking if all conditions have been covered and whether test cases has been written for all the conditions or not. You can use code coverage tools like NCover, NCrunch, dotCover etc.
- View Call Hierarchy (Ctrl + K, Ctrl + T)
To view the Call Hierarchy, use the keyboard shortcut: Ctrl + K, Ctrl + T
- Peek Definition (Alt + F12)
Peek definition is a very popular feature of Visual Studio which allows us to see the definition of the calling member in the same line where it is written. You can use the shortcut: “Alt + F12”.
- Go To Definition (F12)
Click on the method, properties, or type and press F12 to go to definition.
- Go To Implementation ( Ctrl + F12 )
Go to implementation, a new feature in Visual Studio 2015. Before Visual Studio 2015, there was no option to go to the implementation of a method directly and it was very tough if we had to look into implementation of an interface method.
The keyboard shortcut for go to implementation is: Ctrl + F12
- Find All References (Ctrl + K, R)
Finding all references is also a very important feature of VS which allows us to find all the references of types (class, struct, interface etc.) and members (method, property, variables etc.).
The keyboard shortcut for “Find All References” is: Ctrl + K, R