Rectangle text selection in Visual Studio

How and why the rectangular text selection is useful

Home DailyDrop

Daily Knowledge Drop

Holding down the alt key while performing a text selection with the cursor in Visual Studio will allow for a rectangular selection.


Example

The best use case for this feature, is copying commented out code (there are of course many other use cases). For example:

public void LoopAndOutput(int iterations)
{
    //for(int i = 0; i < iterations; i++)
    //{
    //    Console.WriteLine(i);
    //}
}

If wanting to copy the commented out text, the usual steps would be to:

  1. Copy the code with comments
  2. Paste the code into desired location
  3. Either manually remove the forward slash characters, or if pasting into an editor which understands code comments, remove them with the relevant button or keyboard shortcut

Highlighting the text for copying would result in something like this:

Normal text selection

However, an easier way it to use alt + cursor selection. Holding the alt keyword while selecting the text with the cursor will result in this:

Rectangular text selection

The highlighted text is now copied and when pasting, will result in the below. No need to manually remove the double forward slash:

for(int i = 0; i < iterations; i++)
{
    Console.WriteLine(i);
}

Bonus tips

Tip 1

While performing alt + cursor selection one can also drag the cursor vertically to highlight a specific vertical position in the editor, and then edit all highlighted rows at the same time:

Vertical selection

Vertical edit

Tip 2

Using the alt key + the up/down arrows will result in moving the current line(s) up or down.

If multiple lines have been selected, alt + up/down arrow will result in all highlighted lines moving up or down one row.
If no lines are highlighted, the line which contains the cursor I-beam will be moved up or down.


Notes

This is one of the small developer quality of life features I wish I had known about sooner, as I'll certainly be using this almost daily.


Daily Drop 72: 12-05-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net visualstudio selection