Herr Bischoff

Neovim: How to Use init.vim and init.lua Concurrently

Lua configuration support is all the rage in the Neovim community right now. I do see the advantages and welcome the opportunity to dive into Lua.

However, when I add an init.lua file, Neovim loads this one exclusively and I lose all of my current configuration. I do not see myself converting my entire Vim configuration to the new format any time soon. I’m aware of the fallback option to just wrap any raw VimL

vim.cmd([[
set notimeout
set encoding=utf-8
]])

but given that 100% of my existing configuration is in VimL format, that’s just silly. There must be a better way. And there is. Something that was not apparent to me is the ability to add inline Lua code blocks to a VimL file, effectively doing the inverse. Like so:

lua <<EOF

-- Setup packer
local use = require('packer').use
require('packer').startup(function()
  use 'wbthomason/packer.nvim' -- Package manager
  use 'neovim/nvim-lspconfig' -- Configurations for Nvim LSP
end)

-- Setup lspconfig
require'lspconfig'.jedi_language_server.setup{}
require'lspconfig'.tsserver.setup{}

EOF

Note the lua <<EOF and EOF markers. Like doing inline processing in shell scripts.

The minimal example above initializes the packer plugin manager and the Neovim language server configuration plugin. Before this works, you have to manually install packer, which is just a simple git clone away. The lspconfig part tells the nvim-lspconfig plugin to use the Jedi and TypeScript language servers. These need to be manually installed separately. Fortunately, all supported language servers are listed here, along with instructions to install, configure and use them. They could benefit from being grouped by language, though.

While you’re at it, take a look at the suggested configuration. You’ll be using most of the power of VSCode in no time.

If you’re like me and don’t just throw everything into one place, you may want to move this section into a separate file and source it in init.vim:

" Add Lua configuration
source $HOME/.config/nvim/init.lua.vim