From init.vim to init.lua
This is the second article in our series, lets find out how things work in migration!
This is the second article in the series All about Neovim with Lua, if you haven’t read the previous one follow the link here.
Tip: if you have the
nvimdirectories in both
~/.configremove them or backup them because here we will show them from scratch. Example:
We all know that the main configuration file of Vim is
~/.vimrc and its corresponding file in Neovim is
That is, if we create a symlink from vimrc to init.vim your Neovim will work normally as your Vim works and you will also port all settings and plugins to Neovim.
Example, let’s suppose you want to display a message in the footer as soon as you open Neovim, but that message needs to be delivered with the Lua programming language. You just add this code to your
Can you do that with Vim too? If you have compiled your vim with Lua support or added the vim-lua package for systems offering binaries, yes it is possible! But Vim makes a bridge whereas Neovim is native, that is, there is already a good difference and in Vim you should use it like this (as a command):
:lua <<EOF with two dots in front.
But instead of using Here String you can use Lua directly, and this must be done in
init.lua. To use it, remove your
init.vim and create an
Note: Don’t rename or use both (there will be an error) because now the reverse doesn’t work either, that is, VimScript commands don’t run directly in
And to display that same message, just do it like this:
Running VimScript commands in
So, with Lua just run like this, for example, make the Beam cursor in normal mode, add this to your
You can put all of your Vim settings into this function which will work normally.
If you want to create a separate file with your settings and import it to your
init.lua it is more organized, for example: create a file
settings.lua and in your
init.lua just add this line:
The most correct thing is to create a directory with the name lua/ and inside it put all the files that will be imported by
init.lua will automatically read
settings.lua even without specifying the directory because this is part of the
'runtimepath', that is, this directory named lua/ is part of the pattern.
Organization of directories
You can organize it however you like, but there is a default organization. At the moment we still don’t know everything we’ll need to create, but in the next articles we’ll see more details and Neovim’s documentation proposes this hierarchy of organization:
Note: That in this hierarchy the
init.vimfile is kept, but there is an
init.luainside the lua directory, but this is based on if you are not already using
init .luaas main, that is, this is for the transition phase and of course this is the default, but not necessarily a rule.
Here in our series we are going to do similar, but not the same, because not all of these files will be used.
Creating your Lua function in Neovim
At this point you are already thinking about how to deliver VimScript results to Lua. For example, in this article we get the extension of the file that opens every time we use the editor to choose which plugin to load.
Well, getting the extension with Lua is one thing, but getting the extension that Neovim will open is another. We know we could use
vim.cmd, but how to store the output for us to manipulate with Lua ?
Let’s create a new file in the lua/ directory named
nvim lua/tools.lua and inside it we’ll put this code:
Review the comments in the code for greater understanding.
Now open your
init.lua and import
tools.lua like this only:
Now any file we open with Neovim it will tell us the extension at the footer. Simple, right?!
This example was very basic and easy, especially if you already know Lua, but the plugins use a lot of
metatables, however, it doesn’t escape much of simplicity!
For more information run on your Neovim:
:h vim.api .