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
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.
public static void SerializeTickets(List<Ticket> tickets)
{
File.WriteAllText(TicketsFileName, JsonSerializer.Serialize(
tickets, new JsonSerializerOptions { WriteIndented = true }));
}
Place the cursor on WriteIndented when press Shift + Alt + + four times, then Ctrl + C to copy the selected text.
Play around with this keyboard combination. For example a developer wants $"{monthName,15} index is {position}" from below. Use Shift + Alt + + several times than Ctrl + C to copy the selected text.
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 Shift + Alt + ] which will select the entire method.
Not just code, how about json, CSS styles?
Given the following 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 Shift + Alt + ] to select the node.
Same for CSS, given the following (which overrides placeholders for input elements).
::-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 Shift + Alt + ] then Ctrl + C to copy the selected rules.
Same for JavaScript, in this case we want the toggle function.
press Shift + Alt + ] two times to select the function.
$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.
Summary
For keyboard users these keyboard shortcuts can keep a developer from holding down Ctrl and arrow keys to select items.
Top comments (2)
Thank you for sharing this thanks I was also able to discover line duplication by testing your tips
To do this, simply Shift + Alt + Up or Down.
Glad this was helpful, more to follow.