local vim = vim local api = vim.api local M = {} --- function to create a list of commands and convert them to autocommands --- This function is taken from https://github.com/norcalli/nvim_utils function M.nvim_create_augroups(definitions) for group_name, definition in pairs(definitions) do api.nvim_command("augroup " .. group_name) api.nvim_command("autocmd!") for _, def in ipairs(definition) do local command = table.concat(vim.tbl_flatten({ "autocmd", def }), " ") api.nvim_command(command) end api.nvim_command("augroup END") end end local autoCommands = { -- To save the current session (may be restored later). -- autocmd! VimLeavePre * :mks! ~/.config/nvim/session.vim save_session = { { "VimLeavePre", "*", ":mks! ~/.local/state/nvim/session.vim" }, }, -- other autocommands open_folds = { { "BufReadPost,FileReadPost", "*", "normal zR" }, }, } M.nvim_create_augroups(autoCommands) vim.api.nvim_create_autocmd("FileType", { pattern = { "help", "alpha", "dashboard", "neo-tree", "Trouble", "lazy", "mason", "notify", "toggleterm", "lazyterm", }, callback = function(event) vim.b[event.buf].miniindentscope_disable = true end, }) -- ╭──────────────────────────────────────────────────────────╮ -- │ taken from LazyVim repository │ -- ╭─────────────────────────────────────────────────────────────────────────────╮ -- │ https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua│ -- ╰─────────────────────────────────────────────────────────────────────────────╯ local function augroup(name) return vim.api.nvim_create_augroup("ivuorinen_" .. name, { clear = true }) end -- Check if we need to reload the file when it changed vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, { group = augroup("checktime"), command = "checktime", }) -- Highlight on yank vim.api.nvim_create_autocmd("TextYankPost", { group = augroup("highlight_yank"), callback = function() vim.highlight.on_yank() end, }) -- resize splits if window got resized vim.api.nvim_create_autocmd({ "VimResized" }, { group = augroup("resize_splits"), callback = function() local current_tab = vim.fn.tabpagenr() vim.cmd("tabdo wincmd =") vim.cmd("tabnext " .. current_tab) end, }) -- go to last loc when opening a buffer vim.api.nvim_create_autocmd("BufReadPost", { group = augroup("last_loc"), callback = function() local exclude = { "gitcommit" } local buf = vim.api.nvim_get_current_buf() if vim.tbl_contains(exclude, vim.bo[buf].filetype) then return end local mark = vim.api.nvim_buf_get_mark(buf, '"') local lcount = vim.api.nvim_buf_line_count(buf) if mark[1] > 0 and mark[1] <= lcount then pcall(vim.api.nvim_win_set_cursor, 0, mark) end end, }) -- close some filetypes with vim.api.nvim_create_autocmd("FileType", { group = augroup("close_with_q"), pattern = { "PlenaryTestPopup", "help", "lspinfo", "man", "notify", "qf", "spectre_panel", "startuptime", "tsplayground", "neotest-output", "checkhealth", "neotest-summary", "neotest-output-panel", }, callback = function(event) vim.bo[event.buf].buflisted = false vim.keymap.set("n", "q", "close", { buffer = event.buf, silent = true }) end, }) -- wrap and check for spell in text filetypes vim.api.nvim_create_autocmd("FileType", { group = augroup("wrap_spell"), pattern = { "gitcommit", "markdown" }, callback = function() vim.opt_local.wrap = true vim.opt_local.spell = true end, }) -- Auto create dir when saving a file, in case some intermediate directory does not exist vim.api.nvim_create_autocmd({ "BufWritePre" }, { group = augroup("auto_create_dir"), callback = function(event) if event.match:match("^%w%w+://") then return end local file = vim.loop.fs_realpath(event.match) or event.match vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") end, })