In the previous article, Writing Lua in Nyagos to Simulate cmd.exe F1~F9 Functions, the F9 function key was rewritten so that it returns the most recent file name in the current folder. This feature is typically used after downloading a file in the browser, to immediately retrieve the downloaded file name from the command line, thus skipping the dir /od step and the need to manually select and copy.
nyagos.key.F9 = function(this)
_iNewFilenameCounter = 1
local _sFilename = nyagos.eval("ls -t *.* | head -n 1")
if string.sub(_sFilename, -1) == "*" then
_sFilename = _sFilename:sub(1, -2)
end
if string.find(_sFilename, " ") then
_sFilename = '"' .. _sFilename .. '"'
end
nyagos.exec("echo " .. _sFilename .. " | clip") -- convenient for copying
this:call("KILL_LINE")
this:replacefrom(1, _sFilename)
this:call("BEGINNING_OF_LINE")
end
Next, add F10 or Ctrl+Z to select a file in the current folder via an fzf menu. Pressing Enter will return the file name and move the cursor to the very beginning:
-- file selection in current folder
-- | grep -v "^d" removes folders
function list_files(this)
local _sQuery = this:lastword()
local _sCmd = 'll | grep -v "^d" | fzf --cycle --tac --layout=reverse '
if string.len(_sQuery) > 0 then
_sCmd = _sCmd .. "--query=" .. _sQuery
end
-- -rw-a-- 114K Dec 3 12:08:15 Unconfirmed 8091.crdownload
-- tr -s " " compresses multiple spaces into one, ensuring fields separated by spaces are parsed correctly
_sCmd = _sCmd .. ' | tr -s " " | cut -d" " -f6-'
--print("cmd=" .. _sCmd)
local _sFilename = nyagos.eval(_sCmd)
if _sFilename:match("%s") then -- contains spaces
_sFilename = '"' .. _sFilename .. '"'
end
nyagos.exec("echo " .. _sFilename .. " | clip") -- convenient for copying
this:replacefrom(1, _sFilename)
this:call("BEGINNING_OF_LINE")
end
nyagos.key.F10 = list_files
nyagos.key.C_Z = list_files
1. 💡 Related Links
✅ Explanation article (Traditional Chinese): https://jdev.tw/blog/9169/
✅ Explanation article (English)
✅ Explanation article (Japanese)