How to call a lua function in neovim from autocommand or user_command

Viewed 182

I'm trying to migrate some old vimscript to lua. I have a bunch of settings for 'prose' and now have these in .config/nvim/plugin/functions.lua as:

function prose()
vim.o.fdo:append('search')
vim.bo.virtualedit = block
-- more commands
end

Then in prose.lua:

local textedit = vim.api.nvim_create_augroup('textedit', {clear = true})
vim.api.nvim_create_autocmd({"BufEnter", "BufNew"}, {
  group = "textedit",
  pattern = {"*.adoc", "*.md", "*.tex"},
  callback = "prose",
})

vim.api.nvim_create_user_command(
  'Prose',
  "call prose()",
  {nargs = 0, desc = 'Apply prose settings'}
)````

But either the autocommand on opening an .adoc file or running :Prose on the command line will return:
 
````E117: Unknown function: prose````

How can I make my 'prose' function available?
1 Answers

First, your functions.lua file must be in .config/nvim/lua/ directory.

For your autocommand, modify callback to require functions.lua and function prose:

callback = require('functions').prose()

For your user command:

vim.api.nvim_create_user_command('Prose', function()
    require('functions').prose()
  end,                                                                                                                                  
  {nargs = 0, desc = 'Apply prose settings'}                                                                                                       
)      
Related