Access `vim.v.oldfiles` on startup

I trying to access the vim.v.oldfiles in my config.

I created a file nvim/lua/user/restore-on-reopen.lua

local project_root = require("project_nvim.project").get_project_root()
local oldfiles = vim.v.oldfiles
local target = nil

for _, filepath in pairs(oldfiles) do
    if vim.startswith(filepath, project_root) then
        target = filepath

-- Edit the file

Basically it supposed to reopen the last file from oldfiles if it starts with the current project directory.
But when I open vim the vim.v.oldfiles are empty. I tried moving it to nvim/after/plugins but no oldfiles are shown.

The problem is that the ShaDa file (which contains the oldfiles info) is loaded very late into the vim startup file loading process. The only things that load after the ShaDa file is loaded are file-type-specific files and autocommands. So there are two possible ways to solve your problem (that I could think of):

  • add vim.cmd("rshada") befor vim.v.oldfiles
  • Wrap the whole code inside a autocommand: vim.api.nvim_create_autocmd('VimEnter',{callback=function() CODE end})
1 Like