You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have this recurring problem where the shell history contains very long commands which then always turn up as matches because fuzzy matching matches those commands. Now often I don't really need those commands but manually deleting them from the history is a pain.W ith fzf that can be fixed fairly easy though (this is why it's so great:): binding a keyboard shortcut to a custom action which deletes the lines from history. I came up with this:
function Get-UniqueLinesReversed {
$seen = New-Object Collections.Generic.List[String]
foreach ($line in [Linq.Enumerable]::Reverse([IO.File]::ReadAllLines($Args[0]))) {
if (-not $seen.Contains($line)) {
$seen.Add($line)
$line
}
}
}
function fcmdr {
$fzfArgs = @(
"--no-sort",
"--multi", # Not useful when selecting history, but extremely useful for deleting multiple entries with execute()
"--bind",
"backward-eof:abort,ctrl-s:clear-selection,ctrl-d:execute($(Join-Path $PSScriptRoot DelFromHistory.bat) {+f})"
)
Get-UniqueLinesReversed (Get-PSReadlineOption).HistorySavePath | fzf @fzfArgs | ForEach-Object {$result = $_}
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
if ($result) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($result)
}
}
Set-PSReadlineKeyHandler -Key "ctrl-r" -ScriptBlock {fcmdr}
Get-UniqueLinesReversed is similar to what PSFzf uses internally in Invoke-FzfPsReadlineHandlerHistory but uses common functions and is about 3 time faster on my machine, not entirely sure why.
And DelFromHistory.bat is
@echo off
rem Argument is a file where each line has an item which should be deleted from the history file.
rem Note that all matches are deleted
set ARG1=%1
pwsh.exe -NoProfile -NoLogo -Command "&{$i=@(Get-Content %ARG1%); $h=(Get-PSReadLineOption).HistorySavePath; (Get-Content $h) | ?{$_ -notin $i} | Out-File $h -Encoding utf8NoBom}"
This has been super useful so far, despite deleting taking a couple of seconds because launching PS is slow.
I used the separate batch file because I couldn't figure out how to pass the full command to execute directly. I'd like to get rid of the batch file though, anyone has an idea how? Even simple things don't seem to work, probably because the quoting is wrong? E.g.
>> fzf --bind "ctrl-d:execute(pwsh.exe -NoProfile -NoLogo -Command ""&{Out-File -FilePath c:\temp\fzexec 'aa'}"")"
unknown action: execute(pwsh.exe -NoProfile -NoLogo -Command &{Out-File
> fzf --bind "ctrl-d:execute(pwsh.exe -NoProfile -NoLogo -Command """"&{Out-File -FilePath c:\temp\fzexec 'aa'}"""")"
unknown action: execute(pwsh.exe -NoProfile -NoLogo -Command "&{Out-File
> fzf --bind "ctrl-d:execute(pwsh.exe -NoProfile -NoLogo -Command '&{Out-File -FilePath c:\temp\fzexec 'aa'}')"
-> this runs fzf, but hitting ctrl-d says 'Out-file is not recognized as an internal or external command'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have this recurring problem where the shell history contains very long commands which then always turn up as matches because fuzzy matching matches those commands. Now often I don't really need those commands but manually deleting them from the history is a pain.W ith fzf that can be fixed fairly easy though (this is why it's so great:): binding a keyboard shortcut to a custom action which deletes the lines from history. I came up with this:
Get-UniqueLinesReversed is similar to what PSFzf uses internally in Invoke-FzfPsReadlineHandlerHistory but uses common functions and is about 3 time faster on my machine, not entirely sure why.
And DelFromHistory.bat is
This has been super useful so far, despite deleting taking a couple of seconds because launching PS is slow.
I used the separate batch file because I couldn't figure out how to pass the full command to
execute
directly. I'd like to get rid of the batch file though, anyone has an idea how? Even simple things don't seem to work, probably because the quoting is wrong? E.g.Beta Was this translation helpful? Give feedback.
All reactions