Require files not from lua folder

Since Neovim is rapidly moving in a direction of lua instead of vimscript, is there a need for all lua files to be in .config/nvim/lua directory?

If it is already possible to require files just from .config/nvim directory, please guide me how.

If it’s not - than why not? This lua directory seems really excessive.

It is indeed possible to put Lua files (almost) anywhere in your config directory where you can put vimscript files: plugin, after, ftplugin, even colors – no require required (ha!)

The only thing you might want to be aware of that in each directory, Lua files are sourced after all vimscript files (so it’s a.vimb.vima.luab.lua).

But I want to use require, just from other folders. E,g, .config/nvim/lsp/tsserver.lua

Maybe check out Lua Require function on a full path name - Stack Overflow

Then you have to manually add these folders to the Lua package.path; by default, only the lua/ folder is in that list.

I am trying to do the following, but unsuccessful. The file is in ~/.config/nvim/visual-multi.lua

package.path = "~/.config/nvim/;" .. package.path

require("visual-multi")
module 'visual-multi' not found:
        no field package.preload['visual-multi']
        no file '~/.config/nvim/'
        no file './visual-multi.lua'

I don’t think lua interprets the ~ the way you want. Usually, that’s your shell’s doing, and it’s not involved here. Try the full path.

Unfortunately, it didn’t help

E5113: Error while calling lua chunk: error loading module 'visual-multi' from file '/home/user/.config/nvim/':
        cannot read /home/user/.config/nvim/: Is a directory

Ha, progress :slight_smile: At Programming in Lua : 8.1 one can see that you probably want to use

package.path = "/home/user/.config/nvim/?.lua;" .. package.path
1 Like

That worked, thanks!

Do you have any idea how to make it work with relative to home directory path? I want to make it reusable across other devices.

Found. Shell variables can be used

local home_dir = os.getenv("HOME")

If you’re interested in making this portable you might want to look at Nvim documentation: eval (I’m sure there’s a lua variant somewhere)

1 Like