Neovim for Beginners —Lua Autocmd and Keymap Functions

Neovim Lua autocmd and keymap functions.

alpha2phi

--

Neovim for Beginners — Lua Autocmd and Keymap Functions

In our existing Neovim configuration, we use the Vimscript autocmd (:h autocmd) to execute functions and commands in response to some events (:h events). Neovim is getting better in each release and since release 0.7 Lua APIs are provided to configure autocmd the Lua way.

In this article, we are going to explore the Lua autocmd (:h api-autocmd) and keymap(:h lua-keymap) functions.

This article is part of the Neovim for Beginners series.

The Neovim configuration files can be found in this repository.

The Basics

We will only focus on the Lua functions. For the Vimscript basics,

Lua Autocmd Functions

Neovim provides the following autocmd functions (:h api-autocmd).

  • nvim_create_augroup — Create or get an augroup.
  • nvim_create_autocmd — Create an autocmd.
  • nvim_del_augroup_by_id — Delete an augroup by id.
  • nvim_del_augroup_by_name — Delete an augroup by name.
  • nvim_del_autocmd — Delete an autocmd by id.
  • nvim_do_autocmd — Do one autocmd.
  • nvim_get_autocmds — Get autocmds that match the requirements.

We create a new file after/plugins/autocmds.luaand use the nvim_create_augroup and nvim_create_autocmd commands to configure all the autocmds.

Let’s go through several examples.

Yank Highlight

Here is how we configure highlight on yank using Vimscript autocmd.

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.