Switching from VimScript to Lua
Two ways to get started using Lua today, plus a slower approach if that's more your speed
Quick question for you — are you using init.vim
or init.lua
as your entry point for your Neovim configuration?
Serious question — respond to this e-mail or leave a comment. I’d really like to know.
I’ve been talking to folks about Neovim and one of the things that comes up a lot is that people would like to be using Lua but haven’t gotten around to updating their existing Vimscript configuration. So I’m going to be focusing on that problem a bit more for the next few weeks.
Where should I start?
There are three different ways to start using Lua:
Immediately and without any changes, by writing a shim that will load your existing Vimscript config
Immediately, but with some big changes, by switching to a pre-built configuration in Lua
More slowly, but with only the changes you need, by doing a side-by-side rewrite
Switching the Quick Way, with a Shim
If you want to start using Lua to configure your Neovim, like, immediately, here’s a quick hack you can use.
Make a file named init.lua,
put it in your config directory, and then put this line in it:
vim.cmd.source("path/to/my/init.vim")
Congratulations — now you’re using Lua to configure Neovim.
Now you can incrementally move pieces of your configuration out of Vimscript and into your Lua file.
For instance — anywhere your Vimscript, configuration uses set someoption=somevalue
you can instead write something like
vim.o.someoption = somevalue
Pre-Built Configurations
Another approach is to start using a pre-built configuration written in Lua. The best ones here I know about are LunarVim, AstroVim, and LazyVim.
A pre-built configuration is going to take care of a lot of configuration details. They’re great if you’re first learning Neovim and don’t want to have to decide how to setup your bindings. They’ll also protect you somewhat from having to deal with breaking changes in plugins.
The big downside here is that they’ve done all the configuration for you. If you have a bunch of your own keybindings you’ll have to go in and do a bunch of fiddling. You probably won’t understand everything that’s in the configuration or know how to use all of it.
You may also run into platform compatibility issues, especially if you’re running something like FreeBSD. These configurations often use Mason to install packages that Neovim plugins depend on — things like language servers and ripgrep. If you’re particular about how you manage your packages this may be very unwelcome or error prone.
It also won’t necessarily help you if you use a somewhat unusual language. Since I most often am writing Elixir, for instance, pre-built configurations usually don’t do everything I need.
Side-by-Side Rewrite
You might also want to build a totally new configuration and run it side-by-side your existing configuration as you work on it. There are two ways to switch between Neovim configurations.
One is to symlink each of them into your configuration directory, and write a script for switching out the symlinks. This is what I’ve been doing as I work on my example configuration. I have a script in there for setting up the symlinks, and I run it when I want to start working with the new configuration. When I want to switch back to my “main” configuration, I run the script to set up the symlinks there.
You can also pass the configuration that Neovim should use as an argument when you start it, like this:
nvim -u ~/path/to/alternate/config.lua
You can simplify the switching process even more by setting up an alias for the special vim.
alias luavim nvim -u ~/path/to/alternate/config.lua
Are you updating your configuration?
Respond to this e-mail or leave a comment on Substack and let me know
the most recent change you’ve made to your configuration
the next change you’d like to make
The more I know about what you’re working on, the more I can help.