diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2026-02-10 21:25:07 +0000 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2026-02-10 21:25:07 +0000 |
| commit | 7b1bd7d28c402acb644a6df87e750b9a4aff9ac7 (patch) | |
| tree | b9d4c395253307132e8311dc4426b05e60c6e70b | |
| parent | aabcc30883850faec416aa84f7706ddad1795805 (diff) | |
Add autocmds for Neovim
| -rw-r--r-- | nvim/.config/nvim/init.lua | 1 | ||||
| -rw-r--r-- | nvim/.config/nvim/lua/config/autocmds.lua | 98 |
2 files changed, 99 insertions, 0 deletions
diff --git a/nvim/.config/nvim/init.lua b/nvim/.config/nvim/init.lua index 6f51b9a..d18022c 100644 --- a/nvim/.config/nvim/init.lua +++ b/nvim/.config/nvim/init.lua @@ -1,5 +1,6 @@ require('config.globals') require('config.options') require('config.keymaps') +require('config.autocmds') require('config.lsp') require('plugins') diff --git a/nvim/.config/nvim/lua/config/autocmds.lua b/nvim/.config/nvim/lua/config/autocmds.lua new file mode 100644 index 0000000..f93b92f --- /dev/null +++ b/nvim/.config/nvim/lua/config/autocmds.lua @@ -0,0 +1,98 @@ +-- https://raw.githubusercontent.com/LazyVim/LazyVim/refs/heads/main/lua/lazyvim/config/autocmds.lua +local function augroup(name) + return vim.api.nvim_create_augroup('dts_' .. 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'), + callback = function() + if vim.o.buftype ~= 'nofile' then + vim.cmd('checktime') + end + end, +}) + +-- Highlight on yank. +vim.api.nvim_create_autocmd('TextYankPost', { + group = augroup('highlight_yank'), + callback = function() + (vim.hl or 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(event) + local exclude = { 'gitcommit' } + local buf = event.buf + if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].lazyvim_last_loc then + return + end + vim.b[buf].lazyvim_last_loc = true + 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 <q> +vim.api.nvim_create_autocmd('FileType', { + group = augroup('close_with_q'), + pattern = { + 'man', + 'PlenaryTestPopup', + 'checkhealth', + 'dbout', + 'gitsigns-blame', + 'grug-far', + 'help', + 'lspinfo', + 'neotest-output', + 'neotest-output-panel', + 'neotest-summary', + 'notify', + 'qf', + 'spectre_panel', + 'startuptime', + 'tsplayground', + }, + callback = function(event) + vim.bo[event.buf].buflisted = false + vim.schedule(function() + vim.keymap.set('n', 'q', function() + vim.cmd('close') + pcall(vim.api.nvim_buf_delete, event.buf, { force = true }) + end, { + buffer = event.buf, + silent = true, + desc = 'Quit buffer', + }) + end) + 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.uv.fs_realpath(event.match) or event.match + vim.fn.mkdir(vim.fn.fnamemodify(file, ':p:h'), 'p') + end, +}) |
