The functions vim.schedule()
and vim.schedule_wrap()
may be what you are looking for. In short, they schedule a function to somewhere later in the event loop when one can access Neovim's API at a time that is "suitable" for Neovim itself.
To delay the call of your function that is causing issues until 2 seconds after Neovim has started, you could do something like this:
local function start_up_func()
print("My start-up function here")
end
local function delayed_start_up_func()
local timer = vim.loop.new_timer()
-- Delay 2000ms and 0 means "do not repeat"
timer:start(2000, 0, vim.schedule_wrap(start_up_func))
end
delayed_start_up_func()
Alternatively, vim.schedule
without the delay may resolve the issue(s):
local function start_up_func()
print("My start-up function here")
end
vim.schedule(start_up_func)
For more information, have a look at the documentation with h:vim.schedule
and h:vim.schedule_wrap
. This video by TJ DeVries also provides a good overview of these functions and how they can be used.