Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question/Request: Is it possible to remove an item from the history? #1778

Open
DJackman123 opened this issue Aug 28, 2020 · 6 comments
Open
Labels
Issue-Enhancement It's a feature request. Not-Planned
Milestone

Comments

@DJackman123
Copy link

The [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory method makes it really easy to add a command to the history. Is there a corresponding method to remove a command from the history?

I would like to write a function to bind to a key that would recall the last command from the history so I can correct it. Sort of like an UndoLastCommand function. I would use this after entering a command with a misspelling or other syntax problem. Such a command has no useful purpose in the history (I would never want to execute it again).

Hopefully this description makes sense. Is such a thing possible today? Or would this require some coding within the PSReadLine module before I could write a PowerShell script to do this?

@DJackman123 DJackman123 added the Issue-Enhancement It's a feature request. label Aug 28, 2020
@ghost ghost added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Aug 28, 2020
@lzybkr
Copy link
Member

lzybkr commented Aug 28, 2020

Not exactly - you can edit the history file, but I think that would only take effect in new instances of PowerShell.

This wouldn't be too hard to implement in memory, but deleting from the history file gets a bit tricky because of how shared history works - it relies on knowing the length of the history file and knowing that a new command would start at the next offset beyond that length.

@daxian-dbw daxian-dbw removed the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Aug 28, 2020
@theJasonHelmick theJasonHelmick removed the Issue-Enhancement It's a feature request. label Jan 5, 2021
@theJasonHelmick theJasonHelmick removed their assignment Jan 5, 2021
@benallred
Copy link

I'm using this to [kind of] remove an item from the history: https://github.com/benallred/configs/blob/f8814951e66d33feb4ed045b456a2ca60bd46658/powershell/PSReadLine.ps1#L67-L82

It's not perfect, due to the reasons lzybkr mentioned:

  • It doesn't take effect in the current session
    • Oh, well - at least it makes future sessions nicer
  • It results in some extra new lines in the history file when a new command is saved
    • I haven't noticed this cause any problems, though

@camp-007
Copy link

Thanks @benallred for sharing that snippet. That is a very close approximation of the feature I wish was available out of the box. Ideally it would affect the current session, but it is much better than nothing.

FYI, the extra new lines is a quirk of the Set-Content command and can be avoided by changing it to

Set-Content (Get-PSReadLineOption).HistorySavePath $history -NoNewline

@tyg6
Copy link

tyg6 commented Sep 15, 2023

Delete commands by Id number:
Clear-History -Id 3, 5
Clear-History doc

@DJackman123
Copy link
Author

Clear-History is for the wrong history. This post is not talking about the PowerShell session history, but the PSReadLine history that's saved to the (Get-PSReadLineOption).HistorySavePath settting. Clear-History doesn't do anything with that history.

@StevenBucher98 StevenBucher98 added the Issue-Enhancement It's a feature request. label Oct 30, 2023
@StevenBucher98 StevenBucher98 modified the milestones: 2.3.0-Consider, Future Oct 30, 2023
@mikesigs
Copy link

I added this to my profile to help remove items from both history sets.

function Remove-PSReadlineHistory {
    param (
        [Parameter(Mandatory = $true)]
        [string]$Pattern
    )

    $historyPath = (Get-PSReadLineOption).HistorySavePath
    $historyLines = [System.IO.File]::ReadAllLines($historyPath)
    $filteredLines = $historyLines | Where-Object { $_ -notmatch $Pattern }
    [System.IO.File]::WriteAllLines($historyPath, $filteredLines)

    Write-Host "Removed $($historyLines.Count - $filteredLines.Count) line(s) from PSReadLine history."
}

function Remove-PSHistory {
    param (
        [Parameter(Mandatory = $true)]
        [string]$Pattern
    )
    
    $historyLines = Get-History
    $matchingLines = $historyLines | Where-Object { $_.CommandLine -match $Pattern }
    $matchingLines | ForEach-Object { Clear-History -Id $_.Id }
    Write-Host "Removed $($matchingLines.Count) line(s) from PowerShell history."
}

function Remove-History {
    param (
        [Parameter(Mandatory = $true)]
        [string]$Pattern
    )

    Remove-PSReadlineHistory -Pattern $Pattern
    Remove-PSHistory -Pattern $Pattern
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement It's a feature request. Not-Planned
Projects
None yet
Development

No branches or pull requests

9 participants