Skip to content

Instantly share code, notes, and snippets.

@emisjerry
Created November 28, 2025 03:45
Show Gist options
  • Select an option

  • Save emisjerry/c902c33d09e84cbf3a9b26a9ec5b3661 to your computer and use it in GitHub Desktop.

Select an option

Save emisjerry/c902c33d09e84cbf3a9b26a9ec5b3661 to your computer and use it in GitHub Desktop.
Lua scripts for function keys F1~F9, simulating cmd.exe's similar behavior.
-- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment