Back to Blog

Neovim Configuration from Scratch: init.lua, Keymaps & Autocmds | Episode 6

Sandy LaneSandy Lane

Video: Neovim Configuration from Scratch: init.lua, Keymaps & Autocmds | Episode 6 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Neovim Configuration from Scratch: init.lua, Keymaps & Autocmds

Learn how to build a clean and modular Neovim configuration using Lua without relying on any plugins. This guide covers setting up init.lua as the main entry point, organizing your config into separate modules, and customizing options, keymaps, and autocommands for an efficient editing experience.

Code

-- init.lua: main entry point for Neovim configuration
-- Load settings, keymaps, and autocommands from lua/ directory
require("settings")
require("keymaps")
require("autocmds")

-- lua/settings.lua
-- Set leader key before any mappings
vim.g.mapleader = " "

-- Basic editor options
local opt = vim.opt
opt.number = true               -- show absolute line numbers
opt.relativenumber = true       -- show relative line numbers
opt.tabstop = 4                 -- number of spaces per tab
opt.shiftwidth = 4              -- indentation width
opt.expandtab = true            -- use spaces instead of tabs
opt.ignorecase = true           -- case insensitive search
opt.smartcase = true            -- unless uppercase letters used
opt.wrap = false                -- disable line wrapping
opt.scrolloff = 8              -- keep 8 lines visible around cursor
opt.clipboard = "unnamedplus"   -- use system clipboard
opt.undofile = true             -- persistent undo history

-- lua/keymaps.lua
local keymap = vim.keymap.set
local opts = { noremap = true, silent = true }

-- Save and quit mappings with leader key
keymap("n", "w", ":w", opts)      -- Space + w to save
keymap("n", "q", ":q", opts)      -- Space + q to quit

-- Clear search highlight with Escape
keymap("n", "", ":nohlsearch", opts)

-- Scroll and keep cursor centered
keymap("n", "", "zz", opts)         -- Ctrl+d scroll down + center
keymap("n", "", "zz", opts)         -- Ctrl+u scroll up + center

-- Move selected lines up/down in visual mode
keymap("v", "J", ":m '>+1gv=gv", opts)    -- Move selection down
keymap("v", "K", ":m '<-2gv=gv", opts)    -- Move selection up

-- lua/autocmds.lua
local autocmd = vim.api.nvim_create_autocmd
local augroup = vim.api.nvim_create_augroup

local group = augroup("MyAutoCmds", { clear = true })

-- Example: Highlight yanked text briefly
autocmd("TextYankPost", {
  group = group,
  pattern = "*",
  callback = function()
    vim.highlight.on_yank({ timeout = 200 })
  end,
})

Key Points

  • Use init.lua as the configuration entry point and load modular Lua files with require().
  • Set vim.g.mapleader early to define the leader key before any mappings.
  • Configure editor options like line numbers, indentation, search behavior, and clipboard integration via vim.opt.
  • Create keybindings with vim.keymap.set specifying mode, keys, commands, and options for clean and readable mappings.
  • Use autocommand groups and callbacks to automate actions like highlighting yanked text for better visual feedback.