Using peco with Windows (PowerShell) for Incremental History Search
TL;DR
There is an argument that you should just use WSL2 instead of PowerShell. That said, if you are using Windows, you still want to set up a minimal PowerShell environment. Also, WSL2 becomes extremely slow when using external hard drives, which can be quite stressful. When developing on your own PC, storage capacity is a significant concern, and there are times when you want to use an external hard drive.
peco is commonly used with zsh, but it is also a tool that can easily implement incremental search in PowerShell. Since the default Ctrl + r history search is not very user-friendly, let's start by enabling incremental search for command history.
Installing peco
Install using Chocolatey. Administrator privileges are required, so you need to open PowerShell with administrator rights.
Displaying History
PowerShell's input history is saved in a text file at the path shown by (Get-PSReadlineOption).HistorySavePath. So to retrieve history in PowerShell:
We also use Select-Object -Unique because we want the command history to be unique.
Piping to peco and Executing the Selected Result
To execute the selected output, use Invoke-Expression.
So the function to execute is:
That's all we need.
Profile
This is PowerShell's equivalent of .bashrc.
You can check the path or open it with an editor like this.
Write the function from earlier into this file. Now you can use pecoHistory for a nice incremental search experience.
Registering a Keybind
To set keybinds in PowerShell, use Set-PSReadLineKeyHandler. For built-in functions, write them after -Function, but for custom scripts, use -ScriptBlock. Inside the ScriptBlock, we insert the registered pecoHistory command and execute it.
With this setup, you can use Ctrl + r for incremental history search in PowerShell.
References
- Recommendations for PsReadLine Settings (PowerShell)
- Simple Launcher Using peco (PowerShell)
- PowerShell Input History
- Having a Good Day with poco
- Windows PowerShell Profiles
- Set-PSReadLineKeyHandler
- peco
Revision History
- Addressed #65.