Neovim plugins: calling lua

2023-12-19

I recently wrote my first Neovim plugin1 to learn more about how Neovim plugins work and to try to customize my development environment further. It was a bit difficult to get started with developing Neovim plugins and finding good resources, so I will try to summarize what I wish I knew before I got started.

It is relatively simple to call Lua from Neovim

I wanted to write a Neovim plugin for a long while, but postponed it since I thought it would be very difficult. However, Lua is relatively easy to learn and is quite similar to R/Python/Julia making it easy to learn if you come from a data science background. Furthermore, there a lot of Neovim apis which you can take advantage of when writing your plugin, most of which are well documented.

Lua is well integrated into Neovim and is pretty straightforward to call. In fact, you can call Lua directly from the Neovim command mode. The hello world example of this would be:

:lua print('hello world')

Which is very similar to how you would do it in R/Python/Julia.

If you have already setup your init.lua file to configure Neovim, you can define Lua functions directly in your init.lua file. If you for example want to define a hello world function you can copy and paste the following in your init.lua:

function hello(name)
  local message = 'hello ' .. name .. '!' -- Two dots are used to glue strings
  print(message)
end

Which will then enable you to call the following from the Neovim command mode to print ‘hello world!’:

:lua hello('world')

Defining Lua functions and calling them directly from Neovim command mode is pretty straightforward and will hopefully empower you to start defining some functions yourself and dare to think about what types of functions you want to configure.

  1. You can check it out here if you are interested: Mackrics/neoslime