2

I'm trying to see if I can write the output of a command to the prompt buffer to allow for further editing? Something like this, where Write-PromptBuffer is the command desired:

PS C:\> echo "foo bar" | Write-PromptBuffer
PS C:\> foo bar

Something equivalent to what zsh does with print -z (see http://zsh.sourceforge.net/Guide/zshguide03.html)

My main motivation for this is something along the lines of:

PS C:\> Get-Content (Get-PSReadLineOption).HistorySavePath | fzf

Which would "dump" the selected (accepted in fzf lingo) entry as an editable command in the prompt.

(Note: I'm familiar with PSFzf, but I'm trying to find a general purpose command to do this as I have other use cases that would benefit from this)

4
  • How about control r to search for a previous command? Commented Jan 10, 2020 at 21:11
  • That "works" for the specified use-case... even if ctrl-r doesn't do fuzzy match, or show you a list of "currently matching". It does, however, allow you to ctrl-e on the match to be dropped into the prompt with the matched string. I'm looking for something more generic to allow me to do this for not just history look-ups. Commented Jan 10, 2020 at 21:34
  • There's a lot you can do with psreadline key bindings: PSReadlineKeyHandler -bound Plus there's windows, emacs, and vi mode. Commented Jan 10, 2020 at 21:44
  • There is an add-history command... Commented Jan 10, 2020 at 21:56

1 Answer 1

1

The PSReadLine module can insert into the buffer, for example

[Microsoft.PowerShell.PSConsoleReadLine]::Insert("foo bar")

However, as this is intended to edit the current line buffer and not the following line you will need so do something like this

#pipeline to variable $myBuffer function
function Write-PromptBuffer {
    param (  
        [parameter(ValueFromPipeline, ValueFromRemainingArguments = $true)]
        $global:myBuffer
        )
}
#Add a PSReadLineKeyHandler to insert $myBuffer
Set-PSReadLineKeyHandler -Key Alt+x `
    -ScriptBlock {
    param($key, $arg)   # The arguments are ignored in this example
    #write to buffer
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert($myBuffer)
}

Now populate $myBuffer with a string and call it with Alt+w

PS C:\> echo "foo bar" | Write-PromptBuffer
PS C:\> foo bar

Alternatively and not pretty, you could use SendKeys

function Write-PromptBuffer {
    param (  
        [parameter(ValueFromPipeline, ValueFromRemainingArguments = $true)]
        $myBuffer
        )
    (new-object -com wscript.shell).SendKeys($myBuffer)
}

Hope this helps,

Sign up to request clarification or add additional context in comments.

2 Comments

Curious why you say that the SendKeys method is "not pretty". Is there something "morally" wrong about it?
Its difficult to implement safely and often problematic. Keypresses are global so a few Alt+F4's and you'll be shutting down Windows or forget its running a long SendKeys and flip to another application and it'll continue sending. However, for short controlled bursts it might just do what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.