Visual Studio unknown keyboard shortcuts
Tags: csharp, codenewbie, productivity
Published: 2023-09-03 15:13:24

Learn several productivity ways to make life easier with keyboard shortcuts with Microsoft Visual Studio so fingers stay on the keyboard and not the mouse.

**Video is best watched on YouTube**

{% embed https://youtu.be/BxXRux8bars %}

## Expand Selection

Suppose there is a need to select a section of code without the mouse?

Example, select **new JsonSerializerOptions { WriteIndented = true }** from the following code to use in another section of code.

```csharp
public static void SerializeTickets(List<Ticket> tickets)
{
    File.WriteAllText(TicketsFileName, JsonSerializer.Serialize(
        tickets, new JsonSerializerOptions { WriteIndented = true }));
}
```
Place the cursor on **WriteIndented** when press <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>+</kbd> four times, then <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy the selected text.

Play around with this keyboard combination. For example a developer wants **$"{monthName,15} index is {position}"** from below. Use <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>+</kbd> several times than <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy the selected text.

```csharp
var monthName = "November";
if (result.Contains(monthName))
{
    var position = result.IndexOf(monthName);
    if (position >0)
    {
        Console.WriteLine($"{monthName,15} index is {position}");
    }
}
```

## Expand selection to containing block

This allow selecting the parent code similar to Expand Selection shown above. Given the same method used above, press <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>]</kbd> which will select the entire method.

## Not just code, how about json, CSS styles?

Given the following json

```json
[
  {
    "CustomerIdentifier": 1,
    "CompanyName": "Alfreds Futterkiste",
    "ContactId": 1,
    "CountryIdentifier": 9,
    "ContactTypeIdentifier": 1
  },
  {
    "CustomerIdentifier": 2,
    "CompanyName": "Ana Trujillo Emparedados y helados",
    "ContactId": 2,
    "CountryIdentifier": 12,
    "ContactTypeIdentifier": 7
  },
  {
    "CustomerIdentifier": 3,
    "CompanyName": "Antonio Moreno Taquer",
    "ContactId": 3,
    "CountryIdentifier": 12,
    "ContactTypeIdentifier": 7
  },
  {
    "CustomerIdentifier": 4,
    "CompanyName": "Around the Horn",
    "ContactId": 4,
    "CountryIdentifier": 19,
    "ContactTypeIdentifier": 12
  }
]
```

A developer wants to make a copy of **Antonio Moreno Taquer**. Place the cursor in that node and press <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>]</kbd> to select the node.

Same for CSS, given the following (which overrides placeholders for input elements).

```css
::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color: coral
}

:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
    color: coral;
    opacity: 1;
}

::-moz-placeholder { /* Mozilla Firefox 19+ */
    color: coral;
    opacity: 1;
}

:-ms-input-placeholder { /* Internet Explorer 10-11 */
    color: coral;
}

::-ms-input-placeholder { /* Microsoft Edge */
    color: coral;
}

::placeholder { /* Most modern browsers support this now. */
    color: coral;
}

h1 {
    font-size: 18px;
}
```

Place the cursor anywhere in the above section, press <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>]</kbd> then <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy the selected rules.

Same for JavaScript, in this case we want the toggle function.

press <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>]</kbd> two times to select the function.

```javascript
$debugHelper = function () {
    var href = "lib/debugger.css";
    var addCss = function () {
        if (styleStyleIsLoaded(href) === true) {
            return;
        }
        const head = document.head;
        const link = document.createElement("link");
        link.type = "text/css";
        link.rel = "stylesheet";
        link.href = href;
        head.appendChild(link);
    };
    var removeCss = function () {
        if (styleStyleIsLoaded('debugger.css')) {
            document.querySelector(`link[href$="${href}"]`).remove();
        }
    };
    var toggle = function() {
        if (styleStyleIsLoaded(href) === true) {
            removeCss();
        } else {
            addCss();
        }
    }
    var styleStyleIsLoaded = function () {
        for (var index = 0, count = document.styleSheets.length; index < count; index++) {
            const sheet = document.styleSheets[index];
            if (!sheet.href) {
                continue;
            }
            if (sheet.href.includes(href)) {
                return true;
            }
        }
        return false;
    }
    return {
        addCss: addCss,
        removeCss: removeCss,
        isCSSLinkLoaded: styleStyleIsLoaded,
        toggle: toggle
    };
}
```

## Menu items

Most developers do not check out what is available under Visual Studio's Edit menu, Advanced which is where these commands reside.

![Menu items highlighted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/824xajyjhz08gtpwrvli.png)

## Summary

For keyboard users these keyboard shortcuts can keep a developer from holding down <kbd>Ctrl</kbd> and arrow keys to select items.