microsoft/sudo

support `sudo !!` to run the last command

zadjii-msft opened this issue · 4 comments

This is shockingly trivial for CMD.exe. CMD uses the built-in console line-editing, which means that commands run in CMD will use the console's own history buffer. That means sudo can just get the console history with GetConsoleCommandHistory1, to find the last run command.

PowerShell, on the other hand, uses PsReadline in the default configuration. That means we'll need the sudo powershell module (#6) to support sudo !! from powershell.

Footnotes

  1. Is this actually a private API or just an undocumented one? That's not super clear to me looking at https://github.com/microsoft/terminal/blob/ef96e225da6b0df496390eed9fe31dc7e434a939/src/server/ApiSorter.cpp#L109

PowerShell already has $^ to access the last command, so sudo $^ should work too.

I think it'd be a bad idea to support that directly in sudo, rather than supporting it in cmd (so that it works on any command line).

zarlo commented

$^ takes more work to type

Is this actually a private API or just an undocumented one?

GetConsoleCommandHistory is public, in the consoleapi3.h header in the Windows SDK.