Learning Existing and Setting Up New Keyboard Shortcuts in Visual Studio Code for the PowerShell Enthusiast

I’ve recently made myself start using Visual Studio Code for writing some of my PowerShell code and I thought I would share a few of the tips that I’ve learned. If you haven’t read my blog article titled Use the PowerShell Console from within Visual Studio Code, I definitely recommend taking a look at it as today’s blog article assumes that you’ve already made those modifications to your Visual Studio Code environment.

When you first open Visual Studio Code, it defaults to plain text (I haven’t found a way to change this default):

vscode-defaults1a.png

This means that when you start typing in the name of a PowerShell cmdlet such as Get-Service and press tab for tabbed expansion or intellisense as shown in the previous example, it has no idea what you want other than an actual tab.

Of course saving whatever you’re working on as a PS1 file solves this problem, but honestly who saves their PowerShell code before writing anything at all? The trick is to press F1 to show the available Visual Studio Code commands. Start typing language, select Change Language Mode:

vscode-defaults2a.png

As shown in the previous example, you could also use the Ctrl+K M (Ctrl+K then M) hotkey to access Change Language Mode or simply click on Plain Text in the bottom right corner.

Then start typing PowerShell and select PowerShell:

vscode-defaults3a.png

Now your unsaved script knows that you’re writing PowerShell and all of the features such as tabbed expansion and intelllisense work as expected.

vscode-defaults4a.png

I’ve typed in a couple of lines of code that I want to run. Press F1 and type run to see what commands are available:

vscode-defaults5a.png

I’ll select the first one PowerShell: Run selection. The F8 hotkey could also be used to run the selected code:

vscode-defaults6a.png

Some code will run fine, but as shown in the previous example Get-Credential isn’t yet supported.

If you remember in the previous blog article that I referenced, I configured PowerShell.exe as my default integrated terminal for Visual Studio Code. This time I’ll try selecting the last option Terminal: Run Selected Text in Active Terminal to run the selected code in the default integrated terminal:

vscode-defaults7a.png

That worked with the exception of not being able to connect to the specified machine which doesn’t exist so the error this time was expected:

vscode-defaults8a.png

Let’s define a hotkey for running the selected code in the default integrated terminal. Select `File

Preferences > Keyboard Shortcuts`:

vscode-defaults9a.png

I’ll add the necessary code to make Ctrl+Shift+T the hotkey for Terminal: Run Selected Text in Active Terminal:

{
    "key":"ctrl+shift+t",
    "command":"workbench.action.terminal.runSelectedText",
    "when": "editorHasSelection && editorLangId == 'powershell'"
}

vscode-defaults10a.png

Add the code listed in the previous example to the keybindings.json file. Use the Ctrl+S hotkey combination to save the file.

Notice that I was very specific about when that hotkey combination is valid by specifying when in the previous code example. This is because that particular combination is already used in Visual Studio Code and I don’t want to override it in all scenarios, only specific ones. Here’s where it’s already defined:

vscode-defaults14a.png

Now the Ctrl+Shift+T hotkey combination can be used to run the selected code in the default integrated terminal but only when there is a selection in the editor and the language is PowerShell based on how the hotkey is defined:

vscode-defaults11a.png

It would be nice to setup a hotkey to run both Ctrl+I to select the current line and this command I’ve added Ctrl+Shift+T to at the same time similar to the way the PowerShell ISE runs the current line if nothing is selected, but I don’t think it’s currently possible to define a hotkey combination to run two commands.

Interested in discovering what other hotkeys are defined by default in Visual Studio Code?

Open the default keyboard shortcuts again (File > Preferences > Keyboard Shortcuts). Press Ctrl+Shift+O. Start typing in a shortcut to see what it’s mapped to:

vscode-defaults12c.png

One last hotkey shortcut tip is that if you have a folder open in Visual Studio Code, press Ctrl+P which opens a menu at the top that can be used to list recently open files and any file in the open folder. Type in the name of a file to filter the list down and then select an item to open that particular file:

vscode-defaults13a.png

For more information about keyboard shortcuts in Visual Studio Code, see the online key bindings documentation.

ยต