-
Notifications
You must be signed in to change notification settings - Fork 305
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
Temporarily disabling command history #2698
Comments
The default Set-PSReadLineOption -AddToHistoryHandler {
param([string]$line)
return $line.Length -gt 3 -and $line[0] -ne ' ' -and $line[0] -ne ';'
} This lets me put a space at the beginning of the line or a semi-colon at the end to suppress adding to history. It also avoids adding short commands. |
That's good to learn, @lzybkr, it could be handy. It's just hard to sell my office on PowerShell when something like this requires plumbing rather than a real, readable command. |
Commands are easily implemented, or you could wrap Disabling history briefly should feel very interactive, and entering a command to disable, then re-enable feels clunky. |
Whatever the implementation, it will get a lot of use. Extending Set-PSReadLineOption would be a little less readable but I'd be happy enough with it. |
PSReadLine has always had the capability of disabling history (via my suggestion or Also note that my suggestion on ignoring commands starting with a space is exactly what folks coming from bash or zsh are used to. Wrappers might look like: function Disable-PSReadLineHistory {
$global:PSReadLineOldHistorySaveStyle = (Get-PSReadLineOption).HistorySaveStyle
Set-PSReadLineOption -HistorySaveStyle SaveNothing
}
function Enable-PSReadLineHistory {
# Only change the history style if we previously saved it in `Disable-PSReadLineHistory`
if (Test-Path variable:PSReadLineOldHistorySaveStyle) {
Set-PSReadLineOption -HistorySaveStyle $global:PSReadLineOldHistorySaveStyle
}
} |
I guess I just disagree, It's not "good enough as is" and we shouldn't make each user add plumbing for something conceptually simple. |
Maybe a great idea would be to introduce a new context for inputting sensitive commands? Analogous to incognito web browsing experience. |
I agree with @lzybkr's comments above. It can be even simpler as the following, which was already suggested in the stackoverflow question/comments. I think the main confusion comes from the fact that the PSReadLine uses a different history cache/persistence from the built-in PowerShell
|
How's that supposed to work? Also, where can I find this default heuristic? Is it this? PSReadLine/PSReadLine/History.cs Lines 199 to 204 in 0824ad1
|
⚠ "-HistorySaveStyle SaveNothing" does not work in practice
|
@lzybkr @daxian-dbw Would it be an option to treat the space-behavior as sensitive in the default If we use the custom handler as described above, we loose the default behavior for e.g. |
@kapsiR You can save the |
Thanks @daxian-dbw 🙏 For anyone looking for the same: $defaultHistoryHandler = (Get-PSReadLineOption).AddToHistoryHandler;
Set-PSReadLineOption -AddToHistoryHandler {
param([string]$line)
$defaultHandlerResult = $defaultHistoryHandler.Invoke($line)
if ($defaultHandlerResult -eq "MemoryAndFile")
{
return $line.Length -gt 3 -and $line[0] -ne ' ' -and $line[0] -ne ';'
}
return $defaultHandlerResult;
} |
To reiterate my comment from above, the linked example for a custom handler, i.e. Set-PSReadLineOption -AddToHistoryHandler {
param([string]$line)
return $line.Length -gt 3 -and $line[0] -ne ' ' -and $line[0] -ne ';'
} does not work as described
.. unless there's some Because otherwise the check at the beginning of the line is correct, but the semi-colon at the end of the line would be |
Uh, not sure what you mean here. If you use this, nothing gets stored into the on-disk history store of PSReadLine (which you can find with What might be the source of the confusion here are the different history options as defined by this enum here: PS C:\Windows\Temp> [enum]::GetNames([Microsoft.PowerShell.AddToHistoryOption])
SkipAdding
MemoryOnly
MemoryAndFile
PS C:\Windows\Temp> So, there's still the Memory Only type of PSReadLine history, which does not get turned off by the command above, If you want to clear that history (and only the history in memory, not the permanent history stored on disk), simply use this command here: |
@Hrxn Regarding the semi-colon, that should be a typo. You can change it to |
Yes, that's what I was trying to say, basically 😄 |
Cok can sıkıcı geçmiş komutlar silinmiyor sürekli birkaç komut takılı kalıyor vscode kullanıyorum alakalı olabilir mi? |
Description of the new feature/enhancement
It would be nice if there were a concise way to disable/enable recording command history. This would be useful to keep sensitive data out of the history.
I know there's a need for this feature because hundreds of StackOverflow users are looking for a way to remove sensitive data from the history.
Just setting HistorySaveStyle to "SaveNothing" doesn't satisfy this. The sensitive data gets saved again when turning history back on. There's no window of time whose commands don't eventually end up in the history.
Proposed technical implementation details
One idea
The user could sandwich the sensitive command between these calls.
Another idea
Make it part of an existing PSReadLine command. As previously mentioned, disabling/enabling via HistorySaveStyle does not work (sensitive info still gets saved).
The text was updated successfully, but these errors were encountered: