nvim/lua/config/autocmds.lua
Jeremie Fraeys 4742e3a251
Some checks are pending
Check Lua Formatting in MyRepo / Stylua Check (push) Waiting to run
refactor: reorganize config and simplify README
2026-02-08 14:48:48 -05:00

91 lines
2.4 KiB
Lua
Executable file

local function augroup(name)
return vim.api.nvim_create_augroup('nvim_' .. name, { clear = true })
end
-- Auto-format on save
vim.api.nvim_create_autocmd('BufWritePre', {
group = augroup('format_on_save'),
pattern = '*',
callback = function()
vim.lsp.buf.format({ async = false })
end,
desc = 'Auto-format on save with LSP',
})
-- Highlight on yank
vim.api.nvim_create_autocmd('TextYankPost', {
group = augroup('highlight_yank'),
callback = function()
vim.hl.on_yank()
end,
desc = 'Highlight yanked text',
})
-- Remove trailing whitespace
vim.api.nvim_create_autocmd('BufWritePre', {
group = augroup('remove_trailing_whitespace'),
pattern = '*',
command = [[%s/\s\+$//e]],
desc = 'Remove trailing whitespace on save',
})
-- Append newline at EOF (excluding YAML)
vim.api.nvim_create_autocmd('BufWritePre', {
group = augroup('append_newline_eof'),
pattern = '*',
callback = function()
local last_line = vim.fn.getline('$')
local ft = vim.bo.filetype
if last_line ~= '' and not last_line:match('\n$') and ft ~= 'yaml' then
vim.fn.append(vim.fn.line('$'), '')
end
end,
desc = 'Append newline at EOF (except YAML)',
})
-- Auto-update plugins on startup
vim.api.nvim_create_autocmd('VimEnter', {
group = augroup('autoupdate'),
callback = function()
local ok, lazy_status = pcall(require, 'lazy.status')
if ok and lazy_status.has_updates() then
require('lazy').update({ show = false })
end
end,
desc = 'Auto-update plugins on startup',
})
-- Sync plugins after lazy check
vim.api.nvim_create_autocmd('User', {
pattern = 'LazyCheck',
group = augroup('lazy_sync'),
callback = function()
vim.schedule(function()
require('lazy').sync({ wait = false, show = false })
end)
end,
desc = 'Sync plugins after LazyCheck',
})
-- Terminal buffer settings
vim.api.nvim_create_autocmd('TermOpen', {
group = augroup('terminal_settings'),
pattern = '*',
callback = function()
vim.cmd('startinsert')
vim.wo.number = false
vim.wo.relativenumber = false
vim.cmd('normal! G')
end,
desc = 'Terminal buffer auto-insert and no line numbers',
})
-- Terminal mode escape mapping (applied per buffer)
vim.api.nvim_create_autocmd('TermOpen', {
group = augroup('terminal_escape'),
pattern = '*',
callback = function()
vim.keymap.set('t', '<esc><esc>', '<c-\\><c-n>', { buffer = true, desc = 'Exit terminal mode' })
end,
desc = 'Terminal escape mapping',
})