voyeg3r

autocomands

Jul 30th, 2026 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. -- ============================================================================
  2. -- Essential and Modern Neovim Autocommands (Lua API)
  3. -- ============================================================================
  4.  
  5. local function augroup(name)
  6.   return vim.api.nvim_create_augroup("custom_" .. name, { clear = true })
  7. end
  8.  
  9. -- 1. Highlight on Yank
  10. vim.api.nvim_create_autocmd("TextYankPost", {
  11.   group = augroup("highlight_yank"),
  12.   desc = "Highlight text on yank",
  13.   callback = function()
  14.     (vim.hl or vim.highlight).on_yank({
  15.       higroup = "IncSearch",
  16.       timeout = 200,
  17.     })
  18.   end,
  19. })
  20.  
  21. -- 2. Restore Cursor Position
  22. vim.api.nvim_create_autocmd("BufReadPost", {
  23.   group = augroup("restore_cursor"),
  24.   desc = "Restore cursor position when opening a file",
  25.   callback = function(event)
  26.     local exclude = { "gitcommit", "COMMIT_EDITMSG" }
  27.     local ft = vim.bo[event.buf].filetype
  28.  
  29.     if vim.tbl_contains(exclude, ft) or vim.b[event.buf].lazy_user_have_location then
  30.       return
  31.     end
  32.  
  33.     vim.b[event.buf].lazy_user_have_location = true
  34.     local mark = vim.api.nvim_buf_get_mark(event.buf, '"')
  35.     local lcount = vim.api.nvim_buf_line_count(event.buf)
  36.  
  37.     if mark[1] > 0 and mark[1] <= lcount then
  38.       pcall(vim.api.nvim_win_set_cursor, 0, mark)
  39.     end
  40.   end,
  41. })
  42.  
  43. -- 3. Close Auxiliary Windows with <q>
  44. vim.api.nvim_create_autocmd("FileType", {
  45.   group = augroup("close_with_q"),
  46.   desc = "Close utility windows with q",
  47.   pattern = {
  48.     "PlenaryTestPopup",
  49.     "checkhealth",
  50.     "dbout",
  51.     "gitsigns-blame",
  52.     "help",
  53.     "lspinfo",
  54.     "man",
  55.     "neotest-output-panel",
  56.     "qf",
  57.     "query",
  58.     "spectre_panel",
  59.     "startuptime",
  60.     "tsplayground",
  61.   },
  62.   callback = function(event)
  63.     vim.bo[event.buf].buflisted = false
  64.     vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
  65.   end,
  66. })
  67.  
  68. -- 4. Automatically Resize Splits
  69. vim.api.nvim_create_autocmd("VimResized", {
  70.   group = augroup("resize_splits"),
  71.   desc = "Rebalance windows when terminal is resized",
  72.   callback = function()
  73.     local current_tab = vim.fn.tabpagenr()
  74.     vim.cmd("tabdo wincmd =")
  75.     vim.cmd("tabnext " .. current_tab)
  76.   end,
  77. })
  78.  
  79. -- 5. Detect External File Changes
  80. vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
  81.   group = augroup("checktime"),
  82.   desc = "Check if file was modified externally",
  83.   callback = function()
  84.     if vim.o.buftype ~= "nofile" then
  85.       vim.cmd("checktime")
  86.     end
  87.   end,
  88. })
  89.  
  90. -- 6. Create Non-Existent Parent Directories on Save
  91. vim.api.nvim_create_autocmd("BufWritePre", {
  92.   group = augroup("auto_create_dir"),
  93.   desc = "Create missing parent directories before saving",
  94.   callback = function(event)
  95.     if event.match:match("^%w%w+:[\\/]") then
  96.       return
  97.     end
  98.     local file = (vim.uv or vim.loop).fs_realpath(event.match) or event.match
  99.     vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
  100.   end,
  101. })
  102.  
  103. -- 7. Automatic Wrap and Spell for Text Files
  104. vim.api.nvim_create_autocmd("FileType", {
  105.   group = augroup("wrap_spell"),
  106.   desc = "Enable line wrap and spell check for text files",
  107.   pattern = { "gitcommit", "markdown", "text" },
  108.   callback = function()
  109.     vim.opt_local.wrap = true
  110.     vim.opt_local.spell = true
  111.   end,
  112. })
  113.  
  114. -- 8. Remove Trailing Whitespace on Save
  115. vim.api.nvim_create_autocmd("BufWritePre", {
  116.   group = augroup("trim_whitespace"),
  117.   desc = "Remove trailing whitespace before saving",
  118.   callback = function()
  119.     local save_cursor = vim.fn.getpos(".")
  120.     vim.cmd([[%s/\s\+$//e]])
  121.     vim.fn.setpos(".", save_cursor)
  122.   end,
  123. })
  124.  
  125.  
Tags: lua Code tools
Advertisement
Add Comment
Please, Sign In to add comment