-- Autocmds are automatically loaded on the VeryLazy event -- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua -- Add any additional autocmds here local ac = vim.api.nvim_create_autocmd local ag = vim.api.nvim_create_augroup -- Disable diagnostics in a .env file ac("BufRead", { pattern = ".env", callback = function() vim.diagnostic.disable(0) end, }) -- Fix telescope entering on insert mode ac("WinLeave", { callback = function() if vim.bo.ft == "TelescopePrompt" and vim.fn.mode() == "i" then vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "i", false) end end, }) local auto_close_filetype = { "lazy", "mason", "lspinfo", "toggleterm", "null-ls-info", "TelescopePrompt", "notify", } -- Auto close window when leaving ac("BufLeave", { group = ag("lazyvim_auto_close_win", { clear = true }), callback = function(event) local ft = vim.api.nvim_buf_get_option(event.buf, "filetype") if vim.fn.index(auto_close_filetype, ft) ~= -1 then local winids = vim.fn.win_findbuf(event.buf) for _, win in pairs(winids) do vim.api.nvim_win_close(win, true) end end end, }) -- Disable leader and localleader for some filetypes ac("FileType", { group = ag("lazyvim_unbind_leader_key", { clear = true }), pattern = { "lazy", "mason", "lspinfo", "toggleterm", "null-ls-info", "neo-tree-popup", "TelescopePrompt", "notify", "floaterm", }, callback = function(event) vim.keymap.set("n", "", "", { buffer = event.buf, desc = "" }) vim.keymap.set("n", "", "", { buffer = event.buf, desc = "" }) end, }) -- Delete number column on terminals ac("TermOpen", { callback = function() vim.cmd("setlocal listchars= nonumber norelativenumber") end, }) -- Disable next line comments ac("BufEnter", { callback = function() vim.cmd("set formatoptions-=cro") vim.cmd("setlocal formatoptions-=cro") end, }) -- Disable eslint on node_modules ac({ "BufNewFile", "BufRead" }, { group = ag("DisableEslintOnNodeModules", { clear = true }), pattern = { "**/node_modules/**", "node_modules", "/node_modules/*" }, callback = function() vim.diagnostic.disable(0) end, }) -- Use the more sane snippet session leave logic. Copied from: -- https://github.com/L3MON4D3/LuaSnip/issues/258#issuecomment-1429989436 ac("ModeChanged", { pattern = "*", callback = function() if not vim.g.vscode then if ((vim.v.event.old_mode == "s" and vim.v.event.new_mode == "n") or vim.v.event.old_mode == "i") and require("luasnip").session.current_nodes[vim.api.nvim_get_current_buf()] and not require("luasnip").session.jump_active then require("luasnip").unlink_current() end end end, }) local numbertoggle = ag("numbertoggle", { clear = true }) -- Toggle between relative/absolute line numbers ac({ "BufEnter", "FocusGained", "InsertLeave", "CmdlineLeave", "WinEnter" }, { pattern = "*", group = numbertoggle, callback = function() if vim.o.nu and vim.api.nvim_get_mode().mode ~= "i" then vim.opt.relativenumber = true end end, }) ac({ "BufLeave", "FocusLost", "InsertEnter", "CmdlineEnter", "WinLeave" }, { pattern = "*", group = numbertoggle, callback = function() if vim.o.nu then vim.opt.relativenumber = false vim.cmd.redraw() end end, }) -- Create a dir when saving a file if it doesnt exist ac("BufWritePre", { group = ag("auto_create_dir", { clear = true }), callback = function(args) if args.match:match("^%w%w+://") then return end local file = vim.loop.fs_realpath(args.match) or args.match vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") end, })