flake-update-20260201
1-- Autocmds are automatically loaded on the VeryLazy event
2-- Default autocmds: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
3
4local autocmd = vim.api.nvim_create_autocmd
5local augroup = vim.api.nvim_create_augroup
6
7-- Highlight on yank
8autocmd("TextYankPost", {
9 group = augroup("highlight_yank", { clear = true }),
10 callback = function()
11 vim.highlight.on_yank({ higroup = "IncSearch", timeout = 200 })
12 end,
13})
14
15-- Resize splits when window is resized
16autocmd("VimResized", {
17 group = augroup("resize_splits", { clear = true }),
18 callback = function()
19 vim.cmd("tabdo wincmd =")
20 end,
21})
22
23-- Go to last location when opening a buffer
24autocmd("BufReadPost", {
25 group = augroup("last_loc", { clear = true }),
26 callback = function(event)
27 local exclude = { "gitcommit" }
28 local buf = event.buf
29 if vim.tbl_contains(exclude, vim.bo[buf].filetype) then
30 return
31 end
32 local mark = vim.api.nvim_buf_get_mark(buf, '"')
33 local lcount = vim.api.nvim_buf_line_count(buf)
34 if mark[1] > 0 and mark[1] <= lcount then
35 pcall(vim.api.nvim_win_set_cursor, 0, mark)
36 end
37 end,
38})
39
40-- Close some filetypes with <q>
41autocmd("FileType", {
42 group = augroup("close_with_q", { clear = true }),
43 pattern = {
44 "help",
45 "lspinfo",
46 "man",
47 "qf",
48 "query",
49 "spectre_panel",
50 "startuptime",
51 "notify",
52 "checkhealth",
53 },
54 callback = function(event)
55 vim.bo[event.buf].buflisted = false
56 vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
57 end,
58})
59
60-- Auto create dir when saving a file
61autocmd("BufWritePre", {
62 group = augroup("auto_create_dir", { clear = true }),
63 callback = function(event)
64 if event.match:match("^%w%w+://") then
65 return
66 end
67 local file = vim.loop.fs_realpath(event.match) or event.match
68 vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
69 end,
70})