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):
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
:
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:
Now your unsaved script knows that you're writing PowerShell and all of the features such as tabbed expansion and intelllisense work as expected.
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:
I'll select the first one PowerShell: Run selection
. The F8
hotkey could also be used to run the
selected code:
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:
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:
Let's define a hotkey for running the selected code in the default integrated terminal. Select `File
Preferences > Keyboard Shortcuts`:
I'll add the necessary code to make Ctrl+Shift+T
the hotkey for Terminal: Run Selected Text in Active Terminal
:
1{
2 "key":"ctrl+shift+t",
3 "command":"workbench.action.terminal.runSelectedText",
4 "when": "editorHasSelection && editorLangId == 'powershell'"
5}
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:
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:
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:
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:
For more information about keyboard shortcuts in Visual Studio Code, see the online key bindings documentation.
µ