Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active October 30, 2022 05:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaykul/d4fbeea122e46b79f84e9d4cbc768c8b to your computer and use it in GitHub Desktop.
Save Jaykul/d4fbeea122e46b79f84e9d4cbc768c8b to your computer and use it in GitHub Desktop.
# Disable shell integration when the language mode is restricted
# Prevent installing more than once per session
if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage" -or (Test-Path variable:global:__VSCodeOriginalPrompt)) {
return;
}
$Global:__VSCodeOriginalPrompt = $function:Prompt
function global:prompt {
$ExitCode = $? ? 0 : ($LASTEXITCODE -ne 0) ? $LASTEXITCODE : 1
@(
# Prompt started
# OSC 633 ; A ST
"`e]633;A`a"
# Current working directory
# OSC 633 ; <Property>=<Value> ST
if ($pwd.Provider.Name -eq 'FileSystem') { "`e]633;P;Cwd=$($pwd.ProviderPath)`a" }
# Write original prompt
$Global:__VSCodeOriginalPrompt.Invoke()
# Prompt end
# OSC 633 ; B ST
"`e]633;B`a"
) -join ""
}
# If PSReadLine is loaded, wrap it to send the "command executed" sequence first
function global:PSConsoleHostReadLine {
$Command = PSReadLine\PSConsoleHostReadLine
$Command
[Console]::Write("`e]633;C`a")
if ($Command) {
[Console]::Write("`e]633;E;$($Command.Replace("\", "\\").Replace("`n", "\x0a").Replace(";", "\x3b"))`a")
}
}
# Set IsWindows property
[Console]::Write("`e]633;P;IsWindows=$($IsWindows)`a")
# Remap key handlers to the VS Code keybindings
function Copy-PSReadlineKeyHandler {
param ([string[]]$Current, [string[]]$Replacement)
if ($Handler = Get-PSReadLineKeyHandler -Chord $Current | Select-Object -First 1) {
Set-PSReadLineKeyHandler -Chord $Replacement -Function $Handler.Function
}
}
Copy-PSReadlineKeyHandler 'Ctrl+Spacebar' 'F12,a'
Copy-PSReadlineKeyHandler 'Alt+Spacebar' 'F12,b'
Copy-PSReadlineKeyHandler 'Shift+Enter' 'F12,c'
Copy-PSReadlineKeyHandler 'Shift+End' 'F12,d'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment