← Back to overview

Configuration

Location

Neovim looks for configuration at ~/.config/nvim/init.lua (Lua) or ~/.config/nvim/init.vim (Vim script). Lua is the modern, recommended approach.

Minimal init.lua

-- Leader key (prefix for custom mappings)
vim.g.mapleader = " "
vim.g.maplocalleader = " "

-- Line numbers
vim.opt.number = true
vim.opt.relativenumber = true

-- Tabs and indentation
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.expandtab = true
vim.opt.smartindent = true

-- Search
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.hlsearch = true
vim.opt.incsearch = true

-- Appearance
vim.opt.termguicolors = true
vim.opt.scrolloff = 8
vim.opt.signcolumn = "yes"
vim.opt.cursorline = true

-- Behavior
vim.opt.clipboard = "unnamedplus"  -- Use system clipboard
vim.opt.undofile = true            -- Persistent undo
vim.opt.swapfile = false           -- Disable swap files
vim.opt.updatetime = 250           -- Faster completion
vim.opt.timeoutlen = 300           -- Faster key sequence timeout

-- Key mappings
vim.keymap.set("n", "<leader>w", ":w<CR>", { desc = "Save file" })
vim.keymap.set("n", "<leader>q", ":q<CR>", { desc = "Quit" })

-- Clear search highlighting
vim.keymap.set("n", "<leader>h", ":nohlsearch<CR>", { desc = "Clear highlights" })

-- Better window navigation
vim.keymap.set("n", "<C-h>", "<C-w>h", { desc = "Go to left window" })
vim.keymap.set("n", "<C-j>", "<C-w>j", { desc = "Go to lower window" })
vim.keymap.set("n", "<C-k>", "<C-w>k", { desc = "Go to upper window" })
vim.keymap.set("n", "<C-l>", "<C-w>l", { desc = "Go to right window" })

-- Move lines up/down in visual mode
vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv", { desc = "Move selection down" })
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv", { desc = "Move selection up" })

-- Keep cursor centered when scrolling
vim.keymap.set("n", "<C-d>", "<C-d>zz", { desc = "Scroll down and center" })
vim.keymap.set("n", "<C-u>", "<C-u>zz", { desc = "Scroll up and center" })

-- Keep search matches centered
vim.keymap.set("n", "n", "nzzzv", { desc = "Next match and center" })
vim.keymap.set("n", "N", "Nzzzv", { desc = "Previous match and center" })

Understanding vim.opt

In Lua configuration, vim.opt exposes Neovim's options:

vim.opt.number = true          -- Boolean option
vim.opt.tabstop = 4            -- Number option
vim.opt.clipboard = "unnamedplus"  -- String option

Understanding vim.keymap.set

vim.keymap.set(mode, lhs, rhs, opts)
  • mode: "n" (normal), "i" (insert), "v" (visual), "t" (terminal), etc.
  • lhs: the key combination to press
  • rhs: the action to perform
  • opts: table of options like { desc = "..." }