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!
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.
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.
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.
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:
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:
Next, move to the second Console.WriteLine() call, and press Ctrl+Shift+V twice in a row. You’ll get this:
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.
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.
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).
If the cursor is in the middle of some white space, defined as two or more spaces, the white space will be selected.
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.
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.
And after you hit Ctrl+L, the line disappears.
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.
On the Edit–Advanced menu, you’ll find the Delete Horizontal White Space command bound to Ctrl+K, Ctrl+.
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.
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.
Not impressed? You can also drag code to a different file. Drag the code above the desired file tab, as shown next.
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!
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.
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.
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.
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.
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.
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.
Just make sure the cursor is in a text editor to enable these buttons.
3.145.186.83