One would imagine it's a shell feature as it gets the command history from the shell, but unfortunately it's not. Apparently that's a feature of the old conhost.exe terminal, therefore it doesn't work when nesting lots of shell levels or when changing to another unsupported terminal. Windows Terminal supports it for backward compatibility, but will display it differently. You can see many PRs in its source code, like dedup history. Here's one of the most relevant parts
enum class PopupKind
{
// The F2 popup:
// Copies text from the previous command between the current cursor position and the first instance
// of a given char (but not including it) into the current prompt line at the current cursor position.
// Basically, F3 and this prompt have identical behavior, but the prompt searches for a terminating character.
//
// Let's say your last command was:
// echo hello
// And you type the following with the cursor at "^":
// echo abcd efgh
// ^
// Then this command, given the char "o" will turn it into
// echo hell efgh
CopyToChar,
// The F4 popup:
// Erases text between the current cursor position and the first instance of a given char (but not including it).
// It's unknown to me why this is was historically called "copy from char" as it conhost never copied anything.
CopyFromChar,
// The F9 popup:
// Let's you choose to replace the current prompt with one from the command history by index.
CommandNumber,
// The F7 popup:
// Let's you choose to replace the current prompt with one from the command history via a
// visual select dialog. Among all the popups this one is the most widely used one by far.
CommandList,
};
Same to the famous cmder terminal which has explicit support for the popup function keys
This particular feature is not provided by Cmd – it's actually provided by Conhost, the program that draws the 'legacy' console window (and is still involved behind the scenes even if you're using Windows Terminal, although it's known as OpenConsole.exe in that situation). Conhost provides line-editing, history, and alias ("doskey") features for all programs that take line-based input (aka "cooked mode" in Unix terms).
Windows: cmd.exe history (F3, F7, arrow keys, etc) stops working when running cmd.exe nested within itself
If it were a shell feature it'd be in the shell source code instead, and there's none in PowerShell's. As it's so low level, it also explains why I found nothing in the old console API for use from upper user space. Therefore we can only do it manually using the console API
- Get the current texts, back up
- Create another screen buffer, draw the back up background texts
- Draw the popup window, set cursor positions/limit
- Restore the screen buffer when done