Windows Command Prompt cmd.exe common function keys (AI summary):

Function Key Description Example
F1 Copies the previous command one character at a time with each press. Previous command: dir. Press F1 three times → dir
F2 Copies from the previous command up to (but not including) the specified character. Previous command: dir. Press F2, type rdi
F3 Repeats the entire previous command. Previous command: ipconfig. Press F3 → ipconfig
F4 Deletes characters from the cursor position up to (and including) the specified character. Input: cd Program Files, cursor on d. Press F4, type acam Files
F5 Cycles through command history (older commands in sequence). Press F5 repeatedly → shows older commands in order
F6 Inserts ^Z (EOF, End Of File). Press F6 during input → Inserts ^Z and ends input
F7 Displays a list of command history for selection with arrow keys. Press F7 → list appears, select ping google.com
F8 Cycles through command history showing only commands matching current input. Type d, press F8 → displays commands starting with d, e.g., dir
F9 Executes a command from history by its number. Press F7 to view list, if ipconfig is number 3 → Press F9, type 3 → runs ipconfig

I reimplemented similar functionality using the Lua script below:

  • F5: Uses the arrow keys to show command history.
  • F6: No longer used.
  • F7: Uses history | fzf.
  • F9: Returns the most recent filename in the current folder.
-- When you press F2, the command prompt asks you to type a character.
-- It then copies the previous command up to (but not including) that character.
nyagos.key.f2 = function(this)
  local _sCmdLine = this.text;
  if (this.pos > 1) then
    this:call("BEGINNING_OF_LINE")
  end      
  nyagos.write("\nEnter char to copy up to: ")
  local _sKey=nyagos.getkeys()
  nyagos.write("\r\027[K\027[A")
  this:repaint()
  local _iPos = string.find(_sCmdLine, _sKey, 1, true)
  if _iPos then
    for i = 1, _iPos-1 do
      this:call("FORWARD_CHAR")
    end
  end
end

-- F1 → Repeats the last command one character at a time.
nyagos.key.f1 = function(this)
  local _sCmdLine = nyagos.history[#nyagos.history-1]
  local _iPos = this.pos
  _sNewCmdLine = string.sub(_sCmdLine, 1, _iPos)
  this:replacefrom(1,_sNewCmdLine)
end

-- F3 → Repeats the entire last command.
nyagos.key.f3 = function(this)
  local _sCmdLine = nyagos.history[#nyagos.history-1]
  _sNewCmdLine = string.sub(_sCmdLine, 1, _iPos)
  this:replacefrom(1,_sCmdLine)
  this:call("BEGINNING_OF_LINE")
end

-- F4 → Deletes all characters from the current cursor position up to (and including) the next typed character.
-- Example: echo_HelloWorld. _ is the cursor position. 
--   Press F4 & W, Result: echo World.
nyagos.key.f4 = function(this)
  local _sCmdLine = this.text
  local _iCurrentPos = this.pos
  nyagos.write("\nEnter char to delete up to: ")
  local _sKey = nyagos.getkeys()
  nyagos.write("\r\027[K\027[A")
  this:repaint()
  local _iPos = string.find(_sCmdLine, _sKey, 1, true)
  if _iPos then
    local _sNewCmdLine = string.sub(_sCmdLine, 1, _iCurrentPos) .. string.sub(_sCmdLine, _iPos)
    this:call("KILL_LINE")
    this:replacefrom(1,_sNewCmdLine)
    this:call("BEGINNING_OF_LINE")
    for i = 1, _iCurrentPos do
      this:call("FORWARD_CHAR")
    end
  end
end

-- F7 → Shows a list of command history to choose from.
nyagos.key.f7 = function(this)
  this:call("REPAINT_ON_NEWLINE")
  result = nyagos.box(share.__dump_history())
  this:call("REPAINT_ON_NEWLINE")
  return result
end

nyagos.key.END = function(this)
  this:call("END_OF_LINE")
end

-- F9 → Returns the most recent filename.
nyagos.key.F9 = function(this)
  local _sFilename = nyagos.eval("ls -t *.* | head -n 1")
  if string.sub(_sFilename, -1) == "*" then
    _sFilename = _sFilename:sub(1, -2)
  end
  this:replacefrom(1,_sFilename)
  this:call("BEGINNING_OF_LINE")
end

✅ Explanation article (Traditional Chinese): https://jdev.tw/blog/9150/
Explanation article (English)
Explanation article (Japanese)

GitHub my_init.lua