Skip to content

Commit f9d402d

Browse files
authored
Add OSC 133 shell integration sequences for Windows Terminal PowerShell support
2 parents 6678201 + 88ca59f commit f9d402d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ However, Cmder can in fact run in a variety of other terminal emulators, and eve
354354

355355
*Note:* Cmder includes built-in support for Windows Terminal directory tracking via OSC 9;9 sequences. This enables "Duplicate Tab" and "Split Pane" features to preserve the current working directory for both `cmd.exe` and PowerShell sessions.
356356

357+
*Note:* Cmder also includes built-in support for Windows Terminal shell integration via OSC 133 sequences (A, B, C) for PowerShell sessions. This enables features like command navigation (jump between commands), command selection, visual command separators, and improved command history management in Windows Terminal.
358+
357359
For instructions on how to integrate Cmder with your IDE, please read our [Wiki section](https://github.com/cmderdev/cmder/wiki#cmder-integration).
358360

359361
## Upgrading

vendor/profile.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ if (Get-Command -Name "vim" -ErrorAction SilentlyContinue) {
9191

9292
if (Get-Module PSReadline -ErrorAction "SilentlyContinue") {
9393
Set-PSReadlineOption -ExtraPromptLineCount 1
94+
95+
# Add OSC 133;C support for Windows Terminal shell integration
96+
# This marks the start of command output (emitted when Enter is pressed)
97+
if ($env:WT_SESSION) {
98+
Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
99+
# Get the current command line
100+
$line = $null
101+
$cursor = $null
102+
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
103+
104+
# Accept the line first
105+
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
106+
107+
# Emit OSC 133;C sequence to mark start of command output
108+
# This is written directly to the console after the command is accepted
109+
[Console]::Write("$([char]27)]133;C$([char]7)")
110+
}
111+
}
94112
}
95113

96114
# Pre-assign default prompt hooks so the first run of cmder gets a working prompt.
@@ -205,6 +223,14 @@ if ( $(Get-Command prompt).Definition -match 'PS \$\(\$executionContext.SessionS
205223
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]27)]9;9;`"$($loc.ProviderPath)`"$([char]27)\"
206224
}
207225

226+
# Emit OSC 133;A sequence for Windows Terminal shell integration
227+
# This marks the start of the prompt
228+
# Enables features like command navigation, selection, and visual separators
229+
# Only active in Windows Terminal ($env:WT_SESSION)
230+
if ($env:WT_SESSION) {
231+
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]27)]133;A$([char]7)"
232+
}
233+
208234
$host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf
209235
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x200B)`r$([char]0x1B)[K"
210236
if ($lastSUCCESS -or ($LastExitCode -ne 0)) {
@@ -213,6 +239,13 @@ if ( $(Get-Command prompt).Definition -match 'PS \$\(\$executionContext.SessionS
213239
PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
214240
CmderPrompt
215241
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
242+
243+
# Emit OSC 133;B sequence for Windows Terminal shell integration
244+
# This marks the start of command input (after prompt, before user types)
245+
if ($env:WT_SESSION) {
246+
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]27)]133;B$([char]7)"
247+
}
248+
216249
$global:LastExitCode = $realLastExitCode
217250
return " "
218251
}

0 commit comments

Comments
 (0)