Skip to content

Document PowerShell solution for ShellGPT integration#739

Open
jannesbrunner wants to merge 1 commit intoTheR1D:mainfrom
jannesbrunner:patch-1
Open

Document PowerShell solution for ShellGPT integration#739
jannesbrunner wants to merge 1 commit intoTheR1D:mainfrom
jannesbrunner:patch-1

Conversation

@jannesbrunner
Copy link

@jannesbrunner jannesbrunner commented Dec 12, 2025

Summary

This PR adds a PowerShell shell integration example to the README, similar to the existing Bash/Zsh hotkey integration for sgpt.
The example uses PSReadLine to bind a hotkey (Ctrl+G) that:

  • takes the current command line as a natural-language prompt,
  • shows a temporary "loading" message while waiting for sgpt,
  • replaces the entire input line with the generated shell command.

Motivation

Currently, the README documents shell integration for Bash and Zsh, but PowerShell users have to figure out their own setup.
The proposed snippet provides:

  • a simple, copy-pasteable solution for PowerShell,
  • behavior consistent with the Bash/Zsh hotkey workflow,
  • clear UX feedback while the model is generating a command.

This improves the onboarding experience for Windows/PowerShell users.

Implementation

The example uses:

  • PSReadLine to register a key handler (e.g. Ctrl+g),
  • GetBufferState to read the current input line as the prompt,
  • Replace to:
    • first show a (Loading answer... <prompt>) placeholder,
    • then replace it with the sgpt output,
  • sgpt --shell --no-interaction to generate a shell command suitable for direct execution.

Example code to be added to the README:

Import-Module PSReadLine

Set-PSReadLineKeyHandler -Key Ctrl+g -ScriptBlock {
    param($key, $arg)

    # Read current line as natural-language prompt
    $line   = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

    $prompt = $line.Trim()
    if (-not $prompt) { return }

    # Show loading placeholder in the input line
    $placeholder = "(Loading answer... $prompt)"
    [Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $line.Length, $placeholder)
    [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($placeholder.Length)

    $oldPref = $ErrorActionPreference
    $ErrorActionPreference = 'Stop'
    try {
        # Call sgpt to generate a shell command
        $result = & sgpt --shell --no-interaction "$prompt" 2>&1
    }
    catch {
        $ErrorActionPreference = $oldPref

        # Restore original line on error
        $currentLine = $null
        $c = $null
        [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$currentLine, [ref]$c)
        [Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $currentLine.Length, $line)

        Write-Host "Error calling sgpt:" -ForegroundColor Red
        Write-Host $_ -ForegroundColor Red
        return
    }
    $ErrorActionPreference = $oldPref

    if (-not $result) {
        # Restore original line if nothing was returned
        $currentLine = $null
        $c = $null
        [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$currentLine, [ref]$c)
        [Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $currentLine.Length, $line)

        Write-Host "sgpt returned no output." -ForegroundColor Yellow
        return
    }

    # Join multi-line output into a single string (if needed)
    $cmd = ($result -join [Environment]::NewLine)

    # Replace the current line (placeholder) with the generated command
    $currentLine = $null
    $c = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$currentLine, [ref]$c)
    [Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $currentLine.Length, $cmd)
}

Usage

  • Ensure sgpt is installed and available in PATH.
  • Open your PowerShell profile: notepad $PROFILE
  • Paste the above snippet into the profile and save.
  • Restart PowerShell (or run . $PROFILE).
  • Type a natural-language command, e.g.:

"list all files in current folder"

  • Press Ctrl+g:
    The line temporarily becomes:
  • (Loading answer... list all files in current folder)
  • Once sgpt responds, the line is replaced by a Get-ChildItem command that you can review, edit, and execute.

Notes

  • The hotkey (Ctrl+g) can be adjusted by changing the -Key parameter.
  • The loading text can be customized by modifying the $placeholder string.
  • This example targets Windows PowerShell / PowerShell 7+ with PSReadLine available.

Added PowerShell integration script and usage instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant