Neovim Lua Config, Keymaps, and Autocmds

Modern Neovim configuration patterns using Lua, keymaps, options, and autocommands.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Lua options

Set editor options from init.lua and Lua modules.

Enable line numbers

Turn on absolute and relative line numbers.

luaANYneovimluaoptions
lua
vim.opt.number = true
vim.opt.relativenumber = true
Notes

A common navigation-friendly default in modern Neovim configs.

Configure tabs and indentation

Set width and spacing rules.

luaANYneovimluaindentation
lua
vim.opt.tabstop = 2
vim.opt.shiftwidth = 2
vim.opt.expandtab = true
Notes

A foundational code-style block for many projects.

Set the leader key

Define your custom mapping prefix.

luaANYneovimluakeymaps
lua
vim.g.mapleader = " "
Notes

Many Neovim setups use Space as the leader key.

Lua keymaps

Create ergonomic mappings without legacy Vimscript.

Map a save command

Bind a normal-mode key to save the file.

luaANYneovimluakeymaps
lua
vim.keymap.set("n", "<leader>w", "<cmd>write<CR>")
Notes

`vim.keymap.set()` is the recommended API for mappings.

Map Telescope file search

Bind a key to file-finding with a Lua callback.

luaANYneovimluakeymaps
lua
vim.keymap.set("n", "<leader>ff", function()
  require("telescope.builtin").find_files()
end, { desc = "Find files", silent = true })
Notes

Descriptions improve discoverability in helper plugins.

Map line movement in visual mode

Move selected lines up or down.

luaANYneovimluakeymaps
lua
vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv")
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv")
Notes

A common productivity pattern for rearranging blocks.

Lua autocommands

Run logic on editor events with Lua callbacks.

Create an autocmd

Run code when matching files are written.

luaANYneovimluaautocmd
lua
vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = "*.lua",
  callback = function()
    print("saved lua file")
  end,
})
Notes

Neovim’s Lua API makes event handling cleaner than old Vimscript groups.

Create an augroup

Group related autocmds together.

luaANYneovimluaautocmd
lua
local grp = vim.api.nvim_create_augroup("MyGroup", { clear = true })
Notes

Augroups make config idempotent and easier to reload.

Format on save with LSP

Trigger LSP formatting before write.

luaANYneovimluaformatting
lua
vim.api.nvim_create_autocmd("BufWritePre", {
  pattern = "*.ts",
  callback = function()
    vim.lsp.buf.format()
  end,
})
Notes

A common modern workflow for language-aware formatting.

Recommended next

No recommendations yet.