Chapter 1. Get Back to Basics with Your Editor

Performance improvements in Microsoft Visual Studio begin in the editor. There’s no other action you do more than just pure typing, whether you are typing code, editing code, or deleting code. Think of these tips as a coin jar, where you put your spare change. Even if one of these tips saves you a few seconds, those few seconds really start to add up throughout the days, weeks, and months. That’s a lot of spare time saved!

Basic Editing

Regardless of whether you’re coding in C# or editing a plain text file, there are some basic tips you can use for any editing experience.

Text Editing

Over time, we developers form "muscle memory" for how to perform certain tasks, and we do a task in this familiar way even if there’s a more efficient way. For example, consider deleting the current line of text in a file. Your first instinct might be to press Home, then Shift+End, and then Delete. Obviously, this sequence works just fine, and thanks to muscle memory, you’ll never even consider looking up the corresponding keyboard shortcut. But imagine the second or two (or more if you hit the wrong key by accident) you would save if you could press just two keys to perform the same action. Although the amount of time you’ll save might seem small at that moment, consider how the savings can add up if you are constantly deleting lines of text.

Tip 1.1: How to not accidentally copy a blank line

The option that saved my sanity is found in Tools–Options–Text Editor–All Languages–General. There’s a check box called Apply Cut Or Copy Commands To Blank Lines When There Is No Selection. Unchecking this option allows me to press Ctrl+C all I want on a blank line without losing the content on my clipboard.

Sara Aside

Tip 1.2: How to cycle through the Clipboard ring to paste different things

You can cycle through the past 20 items you’ve either cut or copied onto the Clipboard via Ctrl+Shift+V. Pretty cool, huh?

To illustrate, let’s suppose you have two Console.WriteLine() calls and you need to swap the two strings, as shown in the following example:

Sara Aside

Start by cutting both strings: "World" first, and "Hello" second. Now go to the first Console. WriteLine() call. When you press Ctrl+Shift+V once inside the parentheses, you’ll get the following changes to the code:

Sara Aside

Next, move to the second Console.WriteLine() call, and press Ctrl+Shift+V twice in a row. You’ll get this:

Sara Aside

And you store up to 20 items before the Clipboard cycles, meaning that it’ll go back to the first item still recorded on the Clipboard. This is why the feature is called a Clipboard ring.

Tip 1.3: You can use Ctrl+Enter to insert a line above and Ctrl+Shift+Enter to insert a line below

In the following example, note the location of the cursor in the middle of the current line. Pressing Ctrl+Enter inserts a blank line above the current line, and Ctrl+Shift+Enter inserts a blank line below the current line. The cursor moves to the beginning of the new line.

Tip 1.3: You can use Ctrl+Enter to insert a line above and Ctrl+Shift+Enter to insert a line below

Tip 1.4: You can use Ctrl+W to select the current word

Press Ctrl+W at any location on a word to select the entire word. You can have the cursor at the end of word and still have it select the current word (instead of the proceeding white space).

Tip 1.4: You can use Ctrl+W to select the current word

If the cursor is in the middle of some white space, defined as two or more spaces, the white space will be selected.

Tip 1.5: You can use Ctrl+Delete to delete the next word and Ctrl+Backspace to delete the preceding word

Ctrl+Delete deletes the next word the editor finds. The command is Edit.WordDeleteToEnd.

Ctrl+Backspace deletes the previous word. The command is Edit.WordDeleteToStart.

Sara Aside

Tip 1.6: You can use Ctrl+L to cut the current line and Ctrl+Shift+L to delete the current line

Ctrl+L cuts the current line, including the end-of-line character (EOL). The command is Edit. LineCut.

Ctrl+Shift+L deletes the current line, including the EOL. The command is Edit.LineDelete.

Here’s an example of Ctrl+L being used. In this example, you’ll see the cursor before the Console.WriteLine() call.

Tip 1.6: You can use Ctrl+L to cut the current line and Ctrl+Shift+L to delete the current line

And after you hit Ctrl+L, the line disappears.

Tip 1.6: You can use Ctrl+L to cut the current line and Ctrl+Shift+L to delete the current line

But let’s continue on with a bonus tip . . . Shift+Delete cuts the current line, including the EOL, if nothing is selected on the current line. If text is selected, Shift+Delete cuts just that text.

Tip 1.7: How to delete horizontal white space at the beginning of a line

On the Edit–Advanced menu, you’ll find the Delete Horizontal White Space command bound to Ctrl+K, Ctrl+.

Sara Aside

To use, put the cursor anywhere in the white space that precedes the line and press Ctrl+K, Ctrl+. You can also select multiple lines and delete the white space at the beginning of each line.

Sara Aside

Tip 1.8: You can drag code or text to a new location

Select the code block you want to move by holding down the primary mouse button, and then drag the mouse pointer to the desired location. To copy code to the new location, hold down the Ctrl key.

Sara Aside

Not impressed? You can also drag code to a different file. Drag the code above the desired file tab, as shown next.

Sara Aside

Although you’ll get the mouse "can’t drop" pointer, the editor will switch to that file. Then just move the mouse pointer down into the file, and you’ll see the good ol’ "drag and drop" pointer again. Enjoy!

Tip 1.9: You can right-drag code to Move Here or Copy Here

Select a line of code, and then right-drag that line to anywhere within your editor (or into another editor window). Then you’ll get this little menu popup with the options of Move Here, Copy Here, and Cancel.

Sara Aside

Tip 1.10: How to transpose characters, words, and lines in the editor

You can use three commands for transposing or swapping text in the editor, namely:

  • Press Ctrl+T to transpose a character.

  • Press Ctrl+Shift+T to transpose a word.

  • Press Alt+Shift+T to transpose a line.

In the following example (where the cursor is placed before the "is" on the commented line "now is the time"), I’ll apply the three commands to illustrate how text is swapped.

Tip 1.10: How to transpose characters, words, and lines in the editor
  • Pressing Ctrl+T swaps "i" and the previous space, creating "// nowi s the time".

  • Pressing Ctrl+Shift+T swaps "is" and "the", creating "// now the is time".

  • Pressing Alt+Shift+T swaps the current line with the line below it.

Tip 1.11: You can use a keyboard shortcut to uppercase or lowercase a word in the editor

Once again, this tip illustrates that you can save time by using a keyboard shortcut versus having to type out your changes manually.

  • Press Ctrl+Shift+U to make the current character or selected characters uppercase.

  • Press Ctrl+U to make the current character or selected characters lowercase.

Tip 1.11: You can use a keyboard shortcut to uppercase or lowercase a word in the editor

Undo/Redo

In the text editor toolbar, you’ll find the Undo and Redo buttons. But if you look closely, you’ll see a drop-down arrow, meaning that these buttons are actually drop-down controls, displaying your last undo and redo actions.

Tip 1.12: How to use the Undo stack on the standard toolbar

Instead of having to press Ctrl+Z or Ctrl+Y multiple times to undo or redo multiple commands, you can drop down the Undo or Redo button and, starting from the last action, select how many consecutive additional actions you want to undo or redo.

Tip 1.12: How to use the Undo stack on the standard toolbar

Just make sure the cursor is in a text editor to enable these buttons.

..................Content has been hidden....................

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