Learn how to indent and align your code for maximum readability in Visual Studio 2015. Walt show how to visualize the whitespace in code documents, making it easy to find layout problems. See how to configure the tab key in the editor. Find out how Tabify fixes inconsistent whitespace and why smart indents are key to good layout.
- Visual Studio assists you in writing readable code by automatically indenting and aligning your text. In this video, I'll start by showing you how to visualize the whitespace characters in your text editor. If you haven't heard the term whitespace in regard to text editors, it refers to key characters like Tab and Space. These characters are often used by developers to indent and align their code. Being able to see that whitespace can help you solve funky alignment problems. I've opened this Book.cs file. Look at line 11.
You see there's some whitespace at the beginning of that line. There's also some whitespace at the beginning of line 20 but I can't tell whether I use the Tab character or the Space bar to indent the text or whether Visual Studio did it automatically. To visualize the whitespace, I'll open the Edit menu, choose Advanced and then select the View White Space item. If you prefer keyboard shortcuts, you can use the keyboard shortcut listed here. On my settings, it's Control r, Control w Look at line 11 now.
Those blue dots represent spaces. Look at line 20. Those three arrows represent tabs so I can see that I pressed Tab three times on line 20 to indent that text. On line 11, it's a little bit harder to tell. It looks like eight or ten space bars were pressed. Let's see what happens when I move up this line of code and press the Tab key. That's interesting. It indented it another four units but it changed what I had in spaces to tabs.
Now I've got some mixed tabs. I have tabs in line 11. I got spaces on line 12. You scroll down and look here on line 43. I've got two tabs at the beginning of the line and spaces at the end of the line. Nobody likes these mixed tabs and spaces. People like to be consistent so there's a tool I'll show you later called Tabify and Untabify. They can clean this up and make it uniform either all tabs or all spaces. The reason I used the Tab character is because of the settings I have in Tools Option. The options are here in Text Editor.
If you want to affect all the Tab settings for all the languages, use this section. For my demonstration, I'm only going to change the settings for the C Sharp Editor so I'll change them here in this section. So here's what's happening, I have chosen Keep tabs. If I switch this to Insert spaces and return back to Visual Studio and go back up to this line, this line here, now I'm going to press the Tab key. It still indents it four units but now it uses four spaces instead of a single tab character.
I also like to change the tab size. Visual Studio defaults to four for Tab size and four for Indent size. My personal preference is to set this to two units. Why? Because I can see a lot more code on the screen but remember it's your choice how many units to use. So I'll set this to two units and I'll be using this for the rest of this course. And then click on Okay. Now I'll move down to line 13 and press the Tab key and it did put the two spaces in.
I have to press the Tab key again to get it to move over the same as line 12. Here's a common example how some developers like to use indenting. Here on this line of declarations, some people would like to have this variable name line up with the other variable names so then in that case they put the cursor here and then hit the Tab key two times to move those over and then we'll align the numbers and these variables here.
Let's fix these mixed tabs and mixed spaces. That's called Tabify and Untabify so what I'll do is I'll do a Control A to select all the code in my document then I'll move over to Edit, Advanced and select Tabify so that switched all my spaces over to tabs. Since I switched to two units, it's made all my tabs two units in length. Do a Control Z to undo what I just did and now I'll use spaces.
I'll use the Untabify to switch everything back to spaces. Now we've got a consistent set of spaces up and down the code window. So for my final example, I want to talk about this idea of Smart Indenting. This explains what happens when you're on a line of code like 24 or I'm in an if statement and I move down a line. See how it's indented? It's at the curly brace here but when I move down a line, it's indented two extra spaces. If I press the Enter key, you'll see that it's still indented two extra spaces so the Visual Studio rules for C Sharp is when you're inside a set of curly braces, to move over two extra units.
That's done with something called Smart Indenting. I'll show what I mean. Tools, Options. See, I'm set to Smart Indent. If I turn it to no indenting, now when I move down a line, you see where the cursor is? It's at the left margin so it doesn't automatically place it anywhere. My other choice is this one called Block Indenting. So what this means is if I'm on a line and I've indented let's say over to here and I press the Enter key, Block Indenting says to set it back to the block size.
I prefer Smart Indenting and that's what I usually have it set to so I'll set it back before I close this demo. So I'll set this to Smart. I'll use tabs and I'll set my sizes to two then I'll click on Okay. Aligning your code might seem like a minor enhancement but you have to look at your code for hours at a time. Why not get it configured just the way you want? There's a lot more to learn about indenting and formatting your code for readability. That's what I talk about in the next video.
- Exploring the code editor window and default settings
- Commenting code
- Using hover tips and IntelliSense
- Tracking changes
- Refactoring code
- Formatting and arranging code
- Navigating and inspecting code
- Using the Task List
- Working with snippets and smart tags
Skill Level Beginner
Q: This course was updated on 08/21/2017. What changed?
A: New videos were added that cover visualizing code and markup with structure guidelines in Visual Studio 2017; refactor enhancements in Visual Studio 2017; format rules for C# in Visual Studio 2017; format rules for Visual Basic in Visual Studio 2017; smart peek options; and CodeMap in Visual Studio 2017.