Files
nvim/lua/config/autocommands.lua

149 lines
6.4 KiB
Lua
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣷⣦⣄⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣄⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣄⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠚⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠗⢀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣄⡉⠓⠦⣌⡙⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⢋⣡⠴⠚⢉⣠⣤⣀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⣶⣿⣿⣿⣿⣿⣷⣦⣄⡉⠓⠦⣌⡙⠻⢿⣿⣿⣿⣿⣿⣿⡿⠟⢋⣡⠴⠚⢉⣠⣴⣾⣿⣿⣿⣿⣿⣶⣤
⠀⠀⠀⠀⠀⠀⠀⠀⠐⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣄⡉⠓⠦⣌⡙⠻⠟⢋⣡⠴⠚⢉⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⠂
⠀⠀⠀⠀⢀⣠⣴⣆⡈⠻⢿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣄⡉⠃⠘⢉⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⡿⠟⢁⣰⣦⣄⡀
⠀⠀⠐⠾⣿⣿⣿⣿⣿⣷⣦⣌⡙⠻⢿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣿⣿⣷⣾⣿⣿⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⡿⠟⢋⣡⣴⣾⣿⣿⣿⣿⣿⠷⠂
⠀⢸⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣌⡙⠻⢿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⡿⠟⢋⣡⣴⣾⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⡇
⠀⢸⣿⣿⣿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣌⡙⠻⢿⣿⣷⣦⣄⡉⠛⠿⠿⠛⢉⣠⣴⣾⣿⡿⠟⢋⣡⣴⣾⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⣿⣿⣿⡇
⠀⠀⠉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣌⡙⠻⢿⣿⣷⡆⢰⣾⣿⡿⠟⢋⣡⣴⣾⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⣿⣿⣿⣿⠿⠛⠉
⠀⠀⠀⠀⠀⠀⠉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣌⡙⠃⠘⢋⣡⣴⣾⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⣿⣿⣿⣿⠿⠛⠉
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⣿⣷⣾⣿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⣿⣿⣿⣿⠿⠛⠉
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣄⡉⠛⠿⣿⣿⣿⣿⠿⠛⢉⣠⣴⣾⣿⣿⣿⣿⣿⠿⠛⠉
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⣿⣿⣿⣿⣿⣷⣦⣄⡉⢉⣠⣴⣾⣿⣿⣿⣿⣿⠿⠛⠉
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⣿⣿⣿⣿⡇⢸⣿⣿⣿⣿⠿⠛⠉
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⡇⢸⠿⠛⠉
__ _ _
/ _|___ ___| |_ __ _ ___| | __
| ||_ /____/ __| __/ _` |/ __| |/ /
| _/ /_____\__ \ || (_| | (__| <
|_|/___| |___/\__\__,_|\___|_|\_\
Neovim v0.12 configuration
(C) Frank Zechert 2026
]]
local create_group = vim.api.nvim_create_augroup
local create_cmd = vim.api.nvim_create_autocmd
-- briefly highlight text that is yanked
create_cmd("TextYankPost", {
desc = "Highlight yanked text",
group = create_group("cmd-highlight-yank", { clear = true }),
callback = function()
vim.hl.on_yank()
end
})
-- disable list chars in specific file types, e.g. netrw
create_cmd("FileType", {
desc = "Disable list characters for some filetypes",
group = create_group("cmd-disable-listchars", { clear = true }),
pattern = { "netrw", "trouble" },
callback = function(args)
vim.opt_local.list = false
vim.opt.textwidth = 0
end
})
-- when recognizing a bash shebang, switch filetype to bash
create_cmd({"BufRead", "BufNewFile"}, {
desc = "Recognize bash shebang as bash filetype",
group = create_group("cmd-recognize-bash-filetype", { clear = true }),
pattern = "*",
callback = function(args)
local buf = args.buf
local first_line = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1] or ""
if first_line:match("^#!.*/bash") then
vim.bo[buf].filetype = "bash"
end
end
})
-- when saving a file, remove trailing whitespace
create_cmd("BufWritePre", {
desc = "Remove trailing whitespace before saving a file",
group = create_group("cmd-remove-trailing-whitespace", { clear = true }),
pattern = "*",
callback = function(args)
vim.api.nvim_buf_call(args.buf, function()
local cursor = vim.api.nvim_win_get_cursor(0)
vim.cmd([[keepjumps keeppatterns silent! %s/\s\+$//e]])
vim.api.nvim_win_set_cursor(0, cursor)
end)
end
})
-- highlight trailing whitespace when in normal mode
local trailing_hl_group = create_group("cmd-highlight-trailing-whitespace", { clear = true })
local trailing_hl_match_ids = {}
-- Remove highlight in all non-normal modes
create_cmd({"InsertEnter", "CmdlineEnter", "TermEnter" }, {
desc = "Remove trailing whitespace highlight in non-normal mode",
group = trailing_hl_group,
callback = function()
local window = vim.api.nvim_get_current_win()
local id = trailing_hl_match_ids[window]
if id then
pcall(vim.fn.matchdelete, id)
trailing_hl_match_ids[window] = nil
end
end
})
-- Apply highlight in normal mode
create_cmd({"InsertLeave", "CmdlineLeave", "TermLeave", "BufEnter", "WinEnter" }, {
desc = "Add trailing whitespace highlight in normal mode",
group = trailing_hl_group,
callback = function()
if vim.fn.mode() ~= "n" then
return
end
local window = vim.api.nvim_get_current_win()
local id = trailing_hl_match_ids[window]
if id then
pcall(vim.fn.matchdelete, id)
end
trailing_hl_match_ids[window] = vim.fn.matchadd("TrailingWhitespace", [[\s\+$]])
end
})
-- Open Trouble Quickfix list automatically
create_cmd("QuickFixCmdPost", {
desc = "Automatically open Quickfix List in Trouble",
group = create_group("cmd-quickfix-open-trouble", { clear = true }),
callback = function()
vim.cmd([[Trouble qflist open]])
end
})
-- Automatically open diagnostic float when hovering with cursor
create_cmd("CursorHold", {
desc = "Automatically open Diagnostic Float",
group = create_group("cmd-auto-open-diagnostic-float", { clear = true }),
callback = function()
-- If any floating window exists, do nothing
for _, win in ipairs(vim.api.nvim_list_wins()) do
local cfg = vim.api.nvim_win_get_config(win)
if cfg.relative ~= "" then
return
end
end
-- Only open if there is a diagnostic under cursor
local diags = vim.diagnostic.get(0, { lnum = vim.fn.line('.') - 1 })
if #diags == 0 then
return
end
vim.diagnostic.open_float(nil, { focus = false, scope = "cursor" })
end,
})