autocomands.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. -- Create autogroups first
  2. local MZuninoGroup = vim.api.nvim_create_augroup("mzunino", {})
  3. local yank_group = vim.api.nvim_create_augroup("HighlightYank", {})
  4. local bigfile_group = vim.api.nvim_create_augroup("bigfile", {})
  5. -- Set bigfile size threshold
  6. vim.g.bigfile_size = 1024 * 1024 * 1.5 -- 1.5 MB
  7. -- Netrw diagnostic disable
  8. vim.api.nvim_create_autocmd("FileType", {
  9. pattern = "netrw",
  10. callback = function()
  11. vim.diagnostic.enable(false)
  12. end,
  13. })
  14. -- Template files
  15. vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  16. pattern = "*.templ",
  17. command = "set filetype=templ",
  18. })
  19. -- Highlight on yank
  20. vim.api.nvim_create_autocmd("TextYankPost", {
  21. group = yank_group,
  22. pattern = "*",
  23. callback = function()
  24. vim.highlight.on_yank({
  25. higroup = "IncSearch",
  26. timeout = 40,
  27. })
  28. end,
  29. })
  30. -- Remove trailing whitespace on save
  31. vim.api.nvim_create_autocmd("BufWritePre", {
  32. group = MZuninoGroup,
  33. pattern = "*",
  34. command = [[%s/\s\+$//e]],
  35. })
  36. -- Auto-apply chezmoi changes
  37. vim.api.nvim_create_autocmd("BufWritePost", {
  38. group = MZuninoGroup,
  39. pattern = "~/.local/share/chezmoi/*",
  40. command = [[silent! !chezmoi apply --source-path "%"]],
  41. })
  42. -- Bigfile detection
  43. vim.filetype.add({
  44. pattern = {
  45. [".*"] = {
  46. function(path, buf)
  47. return vim.bo[buf].filetype ~= "bigfile" and path and vim.fn.getfsize(path) > vim.g.bigfile_size and "bigfile"
  48. or nil
  49. end,
  50. },
  51. },
  52. })
  53. -- Bigfile handling
  54. vim.api.nvim_create_autocmd("FileType", {
  55. group = bigfile_group,
  56. pattern = "bigfile",
  57. callback = function(ev)
  58. vim.b.minianimate_disable = true
  59. vim.schedule(function()
  60. vim.bo[ev.buf].syntax = vim.filetype.match({ buf = ev.buf }) or ""
  61. end)
  62. end,
  63. })