Windows 命令提示字元 cmd.exe 的常用功能鍵說明如下(AI彙總):

功能鍵 功能說明 使用範例
F1 逐字複製上一條命令,每按一次輸入一個字元 上一命令是 dir,連按三次 F1 → 得到 dir
F2 從上一條命令複製到指定字元為止 上一命令是 dir,按 F2 輸入 r → 得到 di
F3 重複上一條完整命令 上一命令是 ipconfig,按 F3 → 自動輸入 ipconfig
F4 刪除從游標位置到指定字元之間的內容 輸入 cd Program Files,游標在 d,按 F4 輸入 a → 變成 cam Files
F5 循環歷史命令(依序顯示更早的命令) 連續按 F5 → 依序輸入更早的命令
F6 插入 ^Z (EOF,End Of File) 在輸入中按 F6 → 插入 ^Z,結束輸入
F7 顯示命令歷史清單,可用方向鍵選擇 按 F7 → 出現清單,選擇 ping google.com
F8 循環歷史命令(逐一顯示符合目前輸入的命令) 輸入 d,按 F8 → 顯示以 d 開頭的命令,如 dir
F9 依編號執行歷史命令 按 F7 查看清單,假設 ipconfig 是編號 3 → 按 F9 輸入 3 → 執行 ipconfig

我以下列Lua腳本重新實作了類似功能,其中 F5 可以用上下鍵顯示命令歷史,F6不再使用,F7可以用 history | fzf,F9改成傳回當前資料夾裡最新的檔名:

-- 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
  --this:replacefrom(1,key)
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 → It deletes all characters from the current cursor position up to (and including) the next character you type.
-- 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)
    --print("\n\nnew command=" .. _sNewCmdLine)
    this:call("KILL_LINE")
    this:replacefrom(1,_sNewCmdLine)
    
    -- Move cursor back to starting position
    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

-- return the newest 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

1. 💡 相關鏈接

✅ 解說文章(繁體中文):

Explanation article(English)

解説記事(日本語)

GitHub my_init.lua

##