.dotfiles/.config/nvim/lua/autocmd.lua
2026-02-26 01:45:57 +02:00

70 lines
2 KiB
Lua

-- [[ Basic Autocommands ]]
-- See `:help lua-guide-autocommands`
-- Highlight when yanking (copying) text
-- Try it with `yap` in normal mode
-- See `:help vim.highlight.on_yank()`
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Highlight when yanking (copying) text",
group = vim.api.nvim_create_augroup("highlight-yank", { clear = true }),
callback = function()
vim.hl.on_yank()
end,
})
-- vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
-- desc = "diagnostics on hold",
-- group = vim.api.nvim_create_augroup("diagnostics", { clear = true }),
-- callback = function()
-- vim.diagnostic.open_float(nil, { focus = false })
-- end,
-- })
-- TODO: check if it interferes with C-s shortcut
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
desc = "create parent folders automatically",
group = vim.api.nvim_create_augroup("create-parent-automatically", { clear = true }),
callback = function()
vim.cmd(":silent !mkdir -p %:p:h")
end,
})
vim.api.nvim_create_autocmd("BufReadPost", {
pattern = { "*" },
desc = "When editing a file, always jump to the last known cursor position",
group = vim.api.nvim_create_augroup("last-location", { clear = true }),
callback = function()
local line = vim.fn.line("'\"")
if
line >= 1
and line <= vim.fn.line("$")
and vim.bo.filetype ~= "commit"
and vim.fn.index({ "xxd", "gitrebase" }, vim.bo.filetype) == -1
then
vim.cmd('normal! g`"')
end
end,
})
local group = vim.api.nvim_create_augroup("AutoRestartOnConfigChange", { clear = true })
local notify_file = vim.fn.stdpath("cache") .. "/.restart_notify"
vim.api.nvim_create_autocmd("VimEnter", {
group = group,
callback = function()
if vim.fn.filereadable(notify_file) == 1 then
vim.notify("Neovim restarted successfully!", vim.log.levels.INFO)
os.remove(notify_file)
end
end,
})
vim.api.nvim_create_autocmd("BufWritePost", {
group = group,
pattern = vim.fn.stdpath("config") .. "/**",
callback = function()
vim.fn.writefile({}, notify_file)
vim.cmd("AutoSession save")
vim.cmd("restart")
end,
})