local function group(name) return vim.api.nvim_create_augroup('user-' .. name, { clear = true }) end vim.api.nvim_create_autocmd('TextYankPost', { desc = 'Highlight when yanking text', group = group 'yank-highlight', callback = function() vim.hl.on_yank() end, }) -- Pairs with the q: / q/ / q? / mappings that are disabled in keymaps.lua. vim.api.nvim_create_autocmd('CmdwinEnter', { desc = 'Never stay in the command-line window', group = group 'cmdwin', callback = function() vim.cmd 'quit' end, }) vim.api.nvim_create_autocmd('BufReadPost', { desc = 'Jump to the last known cursor position', group = group 'last-position', callback = function(event) local mark = vim.api.nvim_buf_get_mark(event.buf, '"') local lines = vim.api.nvim_buf_line_count(event.buf) if mark[1] > 0 and mark[1] <= lines and vim.bo[event.buf].buftype == '' then pcall(vim.api.nvim_win_set_cursor, 0, mark) end end, }) vim.api.nvim_create_autocmd('BufWritePre', { desc = 'Create missing parent directories on save', group = group 'mkdir', callback = function(event) if event.match:match '^%w%w+:[\\/][\\/]' then return -- remote buffers (scp://, oil-ssh://, ...) end local dir = vim.fn.fnamemodify((vim.uv.fs_realpath(event.match) or event.match), ':p:h') vim.fn.mkdir(dir, 'p') end, }) vim.api.nvim_create_autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, { desc = 'Reload files that changed on disk (git, builds, other sessions)', group = group 'checktime', callback = function() if vim.o.buftype ~= 'nofile' then vim.cmd 'checktime' end end, }) vim.api.nvim_create_autocmd('VimResized', { desc = 'Keep splits equal after the terminal is resized', group = group 'resize', callback = function() local tab = vim.fn.tabpagenr() vim.cmd 'tabdo wincmd =' vim.cmd('tabnext ' .. tab) end, })