The Lua move to ftplugin

I’ve started moving Lua into the ftplugin directory and it’s working well. I have one ftplugin that defines a Lua function that I intend on binding to a normal mode mapping. In a previous life I had done this:

nnoremap('gb', [[:lua require('ftplugin.go').build_files()<CR>]])

However, now I’m not sure how to build this require path, or if its even possible. Any suggestions here?

require(‘ftplugin.go’) can’t load lua file in folder nvim/ftplugin/go.lua.
it will load nvim/lua/ftplugin/go.lua.

So if you want to use a function in /nvim/ftplugin/go.lua you need define a global funciton.

  _G.build_go_files= build_files
   nnoremap('gb', [[:lua build_go_files()<CR>]])
1 Like

This works well, thank you.

You can also add directories to the path:

package.path = package.path .. ';' .. vim.fn.stdpath('config') .. '/ftplugin/?.lua'
require 'go'