Chapter 2. Make Your Editor Work for You

Throughout this book, you’ll watch me go back and forth on what was my favorite feature to test. But I can say without a doubt that there was no other feature I wanted to get my hands on more badly than the editor. Hey, it is the one feature that everyone uses all the time. How could I not want to test it and break it?

Chapter 1, focuses on basic editing tips that can be applied for any file type—even a plain text file. This chapter focuses on basic coding tips for any language file. Here, you will find tips for Microsoft IntelliSense, outlining, and code snippets.

Advanced Editing

This section covers tips you should be aware of when coding. These tips present additional ways to select code, keyboard shortcuts for commenting code, quick mechanisms for formatting your code, and more!

Selection

There are numerous ways to select code beyond the standard mouse drag.

Tip 2.1: How to use box/column selection in the editor

The editor offers two different selection models: stream and box. There’s also line selection, but that’s only in Brief emulations. (See Tip 2.15: How to enable Emacs and Brief editor emulations for more information on editor emulations.)

Stream selection, using Shift+Arrow key, is what everyone is familiar with. But box selection allows you to manually select columns and lines at the same time.

Tip 2.1: How to use box/column selection in the editor

Just hold down Shift+Alt+Arrow key and you’ll quickly get the feel for box selection. You can also use box selection using the mouse by holding down the Alt key while you select text. Cut, Copy, Paste still works, but just keep track of where you started to select the text. Ah, the memories of having to test all this functionality.

Tip 2.2: How to jump to the beginning of some selected text when hitting escape

I hope this tip’s title makes sense. The idea is that you select some text and then hit Escape. Now where do you want the cursor to go?

Tip 2.2: How to jump to the beginning of some selected text when hitting escape

If you want it to stay where it is, that’s the default behavior. But if you want it to jump to the beginning of the selection (that is, the selection anchor), go to Tools–Options–Text Editor–General and check Go To Selection Anchor After Escape.

Tip 2.3: You can use Ctrl+= to select code from the current cursor location to the last go-back marker

See Tip 1.16: How to navigate forward and backward in the editor all because of go-back markers for more information about go-back markers.

The keyboard shortcut used to select code from the current cursor location to the last go-back marker is Ctrl+=, and the command is Edit.SelectToLastGoBack.

Tip 2.3: You can use Ctrl+= to select code from the current cursor location to the last go-back marker

In the previous graphic, I started the cursor at the end of the #Region line and then clicked the mouse about 17 lines down to drop a go-back marker. Then I pressed Ctrl+= to select all the text back to the last go-back marker.

Tip 2.4: How to swap the current anchor position in the editor

Ctrl+K, Ctrl+A will swap the current anchor position.

Cursor position before swap:

Sara Aside

Cursor position after swap:

Sara Aside

Commenting

Now that you know how to quickly select code, you can quickly comment or uncomment the selected lines through keyboard shortcuts.

Tip 2.5: How to quickly comment and uncomment code using keyboard shortcuts

Ah, it really is the simple things in life, isn’t it?

Use Ctrl+K, Ctrl+C to comment code and Ctrl+K, Ctrl+U to uncomment code. All the default development settings have these commands bound to these keyboard shortcuts.

You can find these commands under the Edit–Advanced menu as shown here:

Tip 2.5: How to quickly comment and uncomment code using keyboard shortcuts

Formatting

You can have more control over your cut-and-paste scenarios, whether you are cutting code from a Web site and pasting it into your editor or pasting code into your favorite blog editor. This section presents numerous ways to quickly format your code as needed for a particular language.

Tip 2.6: You can display guidelines in the editor to help format your code

Go to HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0Text Editor.

Create a String (RG_SZ) key called Guides.

The value is in the format of RBG(x,y,z) n1,...,n13, where x,y,z are the RBG values and n is the column number. You can have at most 13 guidelines. For example, RBG(255,0,0) 5,20 will put two red guidelines at column positions 5 and 20, as illustrated here:

Important

Tip 2.7: How to format the document, the selected text, or just the current line

Ever been typing in the editor and, for whatever reason, the text isn’t indented properly on the line? Instead of manually pressing Backspace or Tab for each line of text, just press Ctrl+K, Ctrl+D, which performs the Format Document command.

For larger files, you might just want to select the region that isn’t justified correctly and use Ctrl+K, Ctrl+F. This keyboard shortcut formats the current line if you have nothing selected.

These commands are found under the Edit–Advanced menu.

Tip 2.7: How to format the document, the selected text, or just the current line

Tip 2.8: How to keep tabs or to insert spaces

Go to Tools–Options–Text Editor–<Language>–Tabs to switch between using tabs and inserting spaces.

Sara Aside

Note that you can set this for all languages on the Text Editor–All Languages page, but usually this is something you want to set for each individual language.

Tip 2.9: How to convert spaces to tabs and tabs to spaces

There are four commands that involve converting spaces to tabs and tabs to spaces. The first two commands are found on the Edit–Advanced menu:

  • Tabify Selected Lines. Replaces the leading white space on a line that contains the selection with tabs.

  • Untabify Selected Lines. Replaces the leading white space on a line that contains the selection with spaces.

Untabify Selected Lines

The last two commands are not found on the Edit menu but are available for you to either bind to a keyboard shortcut or manually add to the Edit menu or Text Editor toolbar:

  • Edit.ConvertTabsToSpaces. Converts selected white space to spaces.

  • Edit.ConvertSpacesToTabs. Converts selected white space to tabs.

Edit.ConvertSpacesToTabs

Note that you may have to uncheck Tools–Options–Text Editor–Basic–VB Specific–Pretty Listing (Reformatting) of Code to use Edit.ConvertTabsToSpaces and Edit.ConvertSpacesToTabs.

Tip 2.10: You can increase and decrease the line indent from the text editor toolbar

Sara Aside

With either a single line or several lines selected, you can use either the Increase Indent or Decrease Indent command found on either the text editor toolbar or the Edit–Advanced menu (where it is listed as Increase/Decrease Line Indent).

Tip 2.11: What’s the difference between smart indenting and block indenting?

Tip 2.11: What’s the difference between smart indenting and block indenting?

Smart indenting is the option you want, provided you want the cursor to be properly indented whenever you press Enter or the up and down arrows in the code. An example is when you create a new method called Method1() and then hit Enter. You’ll notice the cursor automatically indents itself. If you continue to hit Enter, the cursor will remain indented. Not all languages support this smart indenting, but if the language you’re using does, this should be the default setting for that language.

Block indenting is similar to a document editor. Using Visual Basic as an example, the difference here is when you type in Method1() and hit Enter, only that first new line is automatically indented. The next new line places the cursor at column 0.

Selecting None will not indent any new lines. You will have to indent everything manually.

Outlining

Take control of outlining, whether you want to quickly collapse or expand your code, or remove it altogether.

Tip 2.12: How to collapse and expand code

There are five commands for outline toggling; they can be found on the Edit–Outlining menu.

Toggle Outlining Expansion

Tip 2.12: How to collapse and expand code

By pressing Ctrl+M, Ctrl+M anywhere within the code block, you can toggle between collapsing a given block of code (as shown here)

Tip 2.12: How to collapse and expand code

and expanding it (as shown next)

Tip 2.12: How to collapse and expand code

Toggle All Outlining

Tip 2.12: How to collapse and expand code

By pressing Ctrl+M, Ctrl+L anywhere in the editor, you can toggle between collapsing and expanding the entire file.

Completely collapsed:

Tip 2.12: How to collapse and expand code

Completely expanded:

Tip 2.12: How to collapse and expand code

Stop Outlining

Tip 2.12: How to collapse and expand code

By pressing Ctrl+M, Ctrl+P anywhere in the editor, you can turn off outlining.

Tip 2.12: How to collapse and expand code

Start Automatic Outlining

Tip 2.12: How to collapse and expand code

Unfortunately, start outlining and stop outlining are not the same command, so you can’t toggle between one state and the other. Additionally, using the General Development Settings, start outlining is not bound to a keyboard shortcut. So you need to go to Edit–Outlining–Start Automatic Outlining to turn on outlining again.

Collapse to Definitions

Tip 2.12: How to collapse and expand code

In my opinion, Collapse To Definitions is is the most useful of all the outlining commands (mostly because it was the only one I used, except for when I had to test the others). This command allows you to quickly glance at all of your functions.

Tip 2.12: How to collapse and expand code

Tip 2.13: You can cut and paste a collapsed block of code

Sara Aside

With a block of code collapsed, as indicated to the right of the code lines shown in the preceding illustration, select the block and cut or just cut the line via your favorite mechanism for cutting a line. Now navigate to the desired location and paste.

Sara Aside

Note the code will be automatically expanded upon pasting.

The idea behind this tip is that you want to quickly cut and paste an entire function, but the function is quite long. You can use the approach shown in the previous tip (specifically, Ctrl+M, Ctrl+M to toggle between expanding and collapsing a block of code) to collapse the function to just the function name. Then press Ctrl+L to cut the current line. Now you can paste the function wherever you want.

Tip 2.14: You can hide outlining (selection margin) without turning off outlining

Go to Tools–Options–Text Editor–General, and uncheck Selection Margin. Although the left margin that indicates a code block is gone, outlining will still work.

Tip 2.14: You can hide outlining (selection margin) without turning off outlining

Emulations

Back in the day, during a summer research program at the University of Massachusetts, I was fortunate enough to use GNU Emacs every day all summer long. I use the word "fortunate" because five years later, I would be trying to "page-in" everything I learned about Emacs to test the Emacs editor emulations in Visual Studio.

The other emulation I tested was Brief. I didn’t have any prior experience with Brief, so I was on my own to take a crash course. Fortunately, there was someone in the Visual Studio building who had used Brief, so I got to bounce a lot of ideas and questions off of him. However, this meant that I had to learn three different sets of keyboard shortcuts! I decided to cycle through the editor emulations, where one week I focused on Emacs, the next week I focused on Brief, and then I had a sanity-check week with the default editor. It was a very confusing time for my muscle memory.

But don’t get me wrong, testing editor emulations was a tester’s corner case heaven. Having been a program manager for a couple of years now, I completely understand what I put the developer and the program manager through by having them figure out what to do with all those bugs I found. But, it was still pure joy.

Tip 2.15: How to enable Emacs and Brief editor emulations

In Visual Studio 2005, we introduced Emacs and Brief emulations into the editor.

Go to Tools–Options–Environment–Keyboard, and then drop down the Apply The Following Additional Keyboard Mapping Scheme list. Then choose either Brief or Emacs.

Tip 2.15: How to enable Emacs and Brief editor emulations

To return to the standard editor, just select (Default).

Binary Editor

You can use the binary editor to edit any resource, provided you want to edit it bit by bit. Yes, I came up with this bad joke all by myself.

Tip 2.16: How to open something in the binary editor

Sara Aside

To use the binary editor, follow these steps:

  1. Go to File–Open File.

  2. Click the drop-down arrow on the Open button or, from the keyboard, just press the down arrow.

  3. Choose Binary Editor and click OK or Open, depending on your Visual Studio version.

    Sara Aside

Delimiter Highlighting and Brace Matching

Ever needed a quick visual check to ensure you are lining up your code correctly? You can use the Automatic Delimiter Highlighting and Brace Matching features to give you that visual cue.

Tip 2.17: What does that Automatic Delimiter Highlighting option do?

Sara Aside

Whenever you have code construct pairs (that’s what the documentation calls them), when you finish typing either the start or end pair, both pairs of words become bold. To turn off this feature, go to Tools–Options–Text Editor–General and uncheck Automatic Delimiter Highlighting.

Sara Aside

You can customize the color for the bolding by going to Tools–Options–Environment–Fonts And Colors and selecting Brace Matching (Highlight).

Sara Aside

And now the #if and #endif appear in bold and in red.

Sara Aside

Tip 2.18: How to change the Brace Matching color

You can change the Brace Matching color. Go to Tools–Options–Environment–Fonts And Colors, select Brace Matching (Rectangle), and set this option to the desired color.

Tip 2.18: How to change the Brace Matching color

And now your curly braces show up with a new highlight color.

Tip 2.18: How to change the Brace Matching color

Clean up unused code

C# provides several interesting features for getting control back over your using statements.

Tip 2.19: You can remove unused using statements

In C#, there’s the option to remove any of your unused using statements. This option is especially helpful if you’re reusing some sort of template over and over again. Instead of having to comment out each line, compile, see whether the compile was successful, and then either remove or uncomment the line, you can bring up the editor context menu (just right-click in the editor) and choose Organize Usings–Remove Unused Usings. This will do all the hard work for you.

Tip 2.19: You can remove unused using statements

AutoRecover

Visual Studio provides an AutoRecover feature to keep you going in the case of an unexpected shutdown, power failure, or some other unfortunate event.

Tip 2.20: What does Visual Studio do to autorecover files in the case of an unexpected shutdown?

Under Tools–Options–Environment, you’ll find the AutoRecover page.

Sara Aside

Note that you can opt out of the AutoRecover feature by unchecking the Save AutoRecover Information Every check box.

The rest of the page is self-explanatory, but did you know where Visual Studio saves these autorecovered files? They are saved in My DocumentsVisual Studio <version>Backup Files<projectname>.

Sara Aside

In case you (hopefully) have never seen the AutoRecover dialog box, here’s what it looks like. It’ll pop up the next time you launch Visual Studio after an unexpected shutdown. You have the options to either use the backup files or ignore them.

Sara Aside

File Extensions

If you ever find yourself editing source code files that use a different file extension, it’s good to know that you can still get syntax highlighting.

Tip 2.21: How to get syntax highlighting for a given file extension

On the Tools–Options–Text Editor–File Extension page, you can map a file extension to one of the included editors.

Tip 2.21: How to get syntax highlighting for a given file extension

And after mapping the .sara extension to a C# editor, we now get syntax highlighting for the file.

Tip 2.21: How to get syntax highlighting for a given file extension

There is also an option to map files without extensions to a specific editor.

Tip 2.21: How to get syntax highlighting for a given file extension
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.25.217