luaneovim

How to properly source/use lua file in `init.vim` config for neovim?


I am new to neovim and I discovered that configs can be written in lua. I have been using init.vim(located at ~/.config/nvim/init.vim), but I wanted to start writing new configs in lua. What is the standard way(or best practices) for sourcing/using lua files in my init.vim ?

At the moment, I have gotten lua running in init.vim script by enclosing lua script between lua <<EOF and EOF tags. However, I don't get any syntax highlighting in vim for my lua code when I do this.


Solution

  • If you're just starting out, instead of init.vim you can place your configuration in .config/nvim/init.lua. E.g. instead of this in init.vim:

    set nocompatible
    set number
    set cmdheight=2
    

    You might have this in init.lua:

    -- This is just a shortcut that allows us to use `o` as an alias for `vim.opt`
    local o = vim.opt
    
    o.compatible = false
    o.number = true
    o.cmdheight = 2
    

    If you have an existing init.vim and you don't want to immediately convert everything into lua, you can add to your init.vim:

    lua require('init')
    

    And this will load the file .config/nvim/lua/init.lua.