43 lines
1.3 KiB
Lua
43 lines
1.3 KiB
Lua
-- core/autocmds.lua
|
|
local aug = vim.api.nvim_create_augroup
|
|
local au = vim.api.nvim_create_autocmd
|
|
|
|
-- highlight yanked text briefly
|
|
au("TextYankPost", {
|
|
group = aug("highlight_yank", { clear = true }),
|
|
callback = function() vim.highlight.on_yank({ timeout = 150 }) end,
|
|
})
|
|
|
|
-- restore last cursor position when reopening a file
|
|
au("BufReadPost", {
|
|
group = aug("last_loc", { clear = true }),
|
|
callback = function(ev)
|
|
local mark = vim.api.nvim_buf_get_mark(ev.buf, '"')
|
|
local lines = vim.api.nvim_buf_line_count(ev.buf)
|
|
if mark[1] > 0 and mark[1] <= lines then
|
|
pcall(vim.api.nvim_win_set_cursor, 0, mark)
|
|
end
|
|
end,
|
|
})
|
|
|
|
-- trim trailing whitespace on save (skip filetypes where it matters)
|
|
au("BufWritePre", {
|
|
group = aug("trim_ws", { clear = true }),
|
|
callback = function()
|
|
if vim.bo.filetype == "diff" or vim.bo.filetype == "markdown" then return end
|
|
local view = vim.fn.winsaveview()
|
|
vim.cmd([[silent! keeppatterns %s/\s\+$//e]])
|
|
vim.fn.winrestview(view)
|
|
end,
|
|
})
|
|
|
|
-- auto-create parent dirs on save
|
|
au("BufWritePre", {
|
|
group = aug("mkdir_on_save", { clear = true }),
|
|
callback = function(ev)
|
|
if ev.match:match("^%w+://") then return end
|
|
local dir = vim.fn.fnamemodify(ev.file, ":p:h")
|
|
if vim.fn.isdirectory(dir) == 0 then vim.fn.mkdir(dir, "p") end
|
|
end,
|
|
})
|