Neovim Configuration and Plugins

Neovim uses LuaJIT (Lua 5.1 semantics). Key APIs: vim.api (core API), vim.fn (Vimscript functions), vim.opt/vim.o (options), vim.keymap, vim.lsp, vim.treesitter, vim.uv (libuv; formerly vim.loop), vim.diagnostic, and vim.tbl_* / vim.iter utilities.

-- ~/.config/nvim/init.lua
vim.g.mapleader = " "
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.expandtab = true
vim.opt.termguicolors = true

vim.keymap.set("n", "<leader>w", "<cmd>write<cr>", { desc = "Save file" })
vim.keymap.set("n", "<leader>q", function() vim.cmd("quit") end, { desc = "Quit" })

vim.api.nvim_create_autocmd("TextYankPost", {
    callback = function() vim.highlight.on_yank({ timeout = 200 }) end,
})

vim.api.nvim_create_user_command("Hello", function(opts)
    print("Hello, " .. (opts.args ~= "" and opts.args or "Neovim"))
end, { nargs = "?" })

-- Plugin manager: lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable", lazypath })
end
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({
    { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate" },
    { "nvim-telescope/telescope.nvim", dependencies = { "nvim-lua/plenary.nvim" } },
    { "neovim/nvim-lspconfig" },
})
-- Writing a plugin: ~/.config/nvim/lua/wordcount/init.lua
local M = {}
function M.count()
    local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
    local words = 0
    for _, line in ipairs(lines) do
        for _ in line:gmatch("%S+") do words = words + 1 end
    end
    vim.notify(("Words: %d"):format(words))
end
function M.setup(opts)
    opts = vim.tbl_deep_extend("force", { key = "<leader>wc" }, opts or {})
    vim.keymap.set("n", opts.key, M.count, { desc = "Word count" })
end
return M
-- usage: require("wordcount").setup()