sudo icon indicating copy to clipboard operation
sudo copied to clipboard

support `sudo !!` to run the last command

Open zadjii-msft opened this issue 1 year ago • 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 GetConsoleCommandHistory[^1], 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.

[^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

zadjii-msft avatar Feb 06 '24 21:02 zadjii-msft

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

jantari avatar Feb 08 '24 21:02 jantari

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).

joshtriplett avatar Feb 09 '24 01:02 joshtriplett

$^ takes more work to type

zarlo avatar Feb 09 '24 04:02 zarlo

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

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

dongle-the-gadget avatar Feb 09 '24 08:02 dongle-the-gadget