local function findfile(fname) local ftype = string.sub(fname, -1) == "/" and "directory" or "file" if fname:sub(0, 1) == "/" then return fname end if ftype == "directory" then fname = fname:sub(0, -2) end local currentfile = vim.api.nvim_buf_get_name(0) local root = vim.fn.isdirectory(currentfile) == 1 and currentfile or vim.fs.dirname(currentfile) local stop = vim.fs.dirname(vim.fn.getcwd(0)) local matches = vim.fs.find(fname, { type = ftype, upward = true, path = root, stop = stop, }) if #matches > 0 then return matches[1] end return vim.fs.joinpath(root, fname) end vim.keymap.set("n", "gf", function() local fname = vim.fn.expand("") ---@cast fname string? if not fname then return end local path = findfile(fname) vim.cmd.edit({ path, magic = { file = false, bar = false } }) end)