Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ============================================================================
- -- Essential and Modern Neovim Autocommands (Lua API)
- -- ============================================================================
- local function augroup(name)
- return vim.api.nvim_create_augroup("custom_" .. name, { clear = true })
- end
- -- 1. Highlight on Yank
- vim.api.nvim_create_autocmd("TextYankPost", {
- group = augroup("highlight_yank"),
- desc = "Highlight text on yank",
- callback = function()
- (vim.hl or vim.highlight).on_yank({
- higroup = "IncSearch",
- timeout = 200,
- })
- end,
- })
- -- 2. Restore Cursor Position
- vim.api.nvim_create_autocmd("BufReadPost", {
- group = augroup("restore_cursor"),
- desc = "Restore cursor position when opening a file",
- callback = function(event)
- local exclude = { "gitcommit", "COMMIT_EDITMSG" }
- local ft = vim.bo[event.buf].filetype
- if vim.tbl_contains(exclude, ft) or vim.b[event.buf].lazy_user_have_location then
- return
- end
- vim.b[event.buf].lazy_user_have_location = true
- local mark = vim.api.nvim_buf_get_mark(event.buf, '"')
- local lcount = vim.api.nvim_buf_line_count(event.buf)
- if mark[1] > 0 and mark[1] <= lcount then
- pcall(vim.api.nvim_win_set_cursor, 0, mark)
- end
- end,
- })
- -- 3. Close Auxiliary Windows with <q>
- vim.api.nvim_create_autocmd("FileType", {
- group = augroup("close_with_q"),
- desc = "Close utility windows with q",
- pattern = {
- "PlenaryTestPopup",
- "checkhealth",
- "dbout",
- "gitsigns-blame",
- "help",
- "lspinfo",
- "man",
- "neotest-output-panel",
- "qf",
- "query",
- "spectre_panel",
- "startuptime",
- "tsplayground",
- },
- callback = function(event)
- vim.bo[event.buf].buflisted = false
- vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
- end,
- })
- -- 4. Automatically Resize Splits
- vim.api.nvim_create_autocmd("VimResized", {
- group = augroup("resize_splits"),
- desc = "Rebalance windows when terminal is resized",
- callback = function()
- local current_tab = vim.fn.tabpagenr()
- vim.cmd("tabdo wincmd =")
- vim.cmd("tabnext " .. current_tab)
- end,
- })
- -- 5. Detect External File Changes
- vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
- group = augroup("checktime"),
- desc = "Check if file was modified externally",
- callback = function()
- if vim.o.buftype ~= "nofile" then
- vim.cmd("checktime")
- end
- end,
- })
- -- 6. Create Non-Existent Parent Directories on Save
- vim.api.nvim_create_autocmd("BufWritePre", {
- group = augroup("auto_create_dir"),
- desc = "Create missing parent directories before saving",
- callback = function(event)
- if event.match:match("^%w%w+:[\\/]") then
- return
- end
- local file = (vim.uv or vim.loop).fs_realpath(event.match) or event.match
- vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
- end,
- })
- -- 7. Automatic Wrap and Spell for Text Files
- vim.api.nvim_create_autocmd("FileType", {
- group = augroup("wrap_spell"),
- desc = "Enable line wrap and spell check for text files",
- pattern = { "gitcommit", "markdown", "text" },
- callback = function()
- vim.opt_local.wrap = true
- vim.opt_local.spell = true
- end,
- })
- -- 8. Remove Trailing Whitespace on Save
- vim.api.nvim_create_autocmd("BufWritePre", {
- group = augroup("trim_whitespace"),
- desc = "Remove trailing whitespace before saving",
- callback = function()
- local save_cursor = vim.fn.getpos(".")
- vim.cmd([[%s/\s\+$//e]])
- vim.fn.setpos(".", save_cursor)
- end,
- })
Advertisement
Add Comment
Please, Sign In to add comment