Crates.io | posh-fzf |
lib.rs | posh-fzf |
version | 0.2.8 |
source | src |
created_at | 2023-11-10 14:08:54.87529 |
updated_at | 2023-12-10 13:25:17.618154 |
description | Fzf keybinding integration for powershell |
homepage | |
repository | https://github.com/domsleee/posh-fzf |
max_upload_size | |
id | 1031171 |
size | 211,312 |
Fzf keybinding integration with powershell 5.1 and pwsh >= 7.
cargo install posh-fzf
, or download the latest release from releases.fd
and fzf
.
scoop install fzf fd
.code $PROFILE
:Invoke-Expression (&posh-fzf init | Out-String)
# Customize the key bindings to your liking
Set-PSReadLineKeyHandler -Key 'Ctrl+t' -ScriptBlock { Invoke-PoshFzfSelectItems }
Set-PSReadLineKeyHandler -Key 'Alt+c' -ScriptBlock { Invoke-PoshFzfChangeDirectory }
Set-PSReadLineKeyHandler -Key 'Ctrl+r' -ScriptBlock { Invoke-PoshFzfSelectHistory }
Command | Description |
---|---|
Invoke-PoshFzfSelectItems |
Select one or more items and paste it to the terminal. |
Invoke-PoshFzfChangeDirectory |
Show all child directories, and if selected, cd to that directory. |
Invoke-PoshFzfSelectHistory |
Show distinct historical commands in most recent order. |
Invoke-PoshFzfStartProcess |
Invokes a process using the Process API. See custom commands below. |
Invoke-PoshFzfInsertUtf8 |
Inserts text forcing UTF-8 encoding. |
Invoke-PoshFzfTempEnv |
Temporarily set environment variables, and reset them after executing a script block. |
You can use any fzf
command in keybindings by tweaking it to use the Invoke-PoshFzf
cmdlet.
[!NOTE]
-HeightRowsOrPercent
must match the--height
argument of fzf. This is due tofzf
and PSReadline not working well together iffzf
has to scroll the terminal down when it is invoked.
For example, instead of git branch | fzf
, you would have:
Set-PSReadLineKeyHandler -Key 'Alt+b' -ScriptBlock {
$branch = git branch | Invoke-PoshFzfStartProcess -FileName "fzf" -Arguments @("--height=45%") -HeightRowsOrPercent "45%"
if ($branch) {
$branch = $branch.Replace("*", "").Trim()
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine()
Invoke-PoshFzfInsertUtf8("git checkout '$branch'")
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
}
You can also executables other than fzf
.
For example, zoxide provides a way to inspect it's db using zoxide query -i
. Internally, it calls fzf
.
Instead of $selection = (zoxide query -i)
, it can be called like this:
Set-PSReadLineKeyHandler -Key 'Ctrl+shift+z' -ScriptBlock {
# 45% height comes from: https://github.com/ajeetdsouza/zoxide/blob/a624ceef54a31de2d0624e9eb14ce65024cc9e79/src/cmd/query.rs#L92
$fzfSelection = Invoke-PoshFzfStartProcess -FileName "zoxide" -Arguments @("query", "-i") -HeightRowsOrPercent "45%"
if ($fzfSelection) {
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine()
Invoke-PoshFzfInsertUtf8("cd $fzfSelection")
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
}
You can set FZF_DEFAULT_COMMAND
temporarily like this, the example is from posh-fzf.ps1
function Invoke-PoshFzfChangeDirectory {
Invoke-PoshFzfTempEnv @{FZF_DEFAULT_COMMAND = 'fd --type d --hidden --exclude ".git"'} -ScriptBlock {
$directory = Invoke-PoshFzfStartProcess -FileName "fzf" -Arguments @("$defaultHeightArg", "--preview", "fd . {} --maxdepth 1")
if ($directory) {
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine()
Invoke-PoshFzfInsertUtf8 "cd $directory"
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
}
}