summaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authortdback <tyler@tdback.net>2025-01-17 16:47:43 -0500
committertdback <tyler@tdback.net>2025-01-17 16:47:43 -0500
commit7780e97dd26aae185cb6714820f274f1e35f1439 (patch)
tree00cfa63ad41d885c97675eb48efc43fac6df08d6 /plugin
parentfba1eaddfb205f57ccf29dff7f1d7e5dd97709b7 (diff)
feat: add some QoL autocmds and cleanup existing
Diffstat (limited to 'plugin')
-rw-r--r--plugin/autocmd.lua50
1 files changed, 48 insertions, 2 deletions
diff --git a/plugin/autocmd.lua b/plugin/autocmd.lua
index 21b5f9f..1597a21 100644
--- a/plugin/autocmd.lua
+++ b/plugin/autocmd.lua
@@ -1,7 +1,7 @@
local autocmd = vim.api.nvim_create_autocmd
+-- Highlight text being yanked.
autocmd({ "TextYankPost" }, {
- pattern = "*",
callback = function()
vim.highlight.on_yank({
higroup = "IncSearch",
@@ -10,4 +10,50 @@ autocmd({ "TextYankPost" }, {
end,
})
-autocmd({ "BufWritePre" }, { pattern = "*", command = [[%s/\s\+$//e]] })
+-- Remove trailing whitespace on save (and keep cursor's position!).
+autocmd({ "BufWritePre" }, {
+ callback = function()
+ local cursor = vim.fn.getpos(".")
+ vim.cmd([[%s/\s\+$//e]])
+ vim.fn.setpos(".", cursor)
+ end,
+})
+
+-- Restore cursor's position in buffer from previous session.
+autocmd({ "BufReadPost" }, {
+ callback = function(args)
+ local mark = vim.api.nvim_buf_get_mark(args.buf, '"')
+ local count = vim.api.nvim_buf_line_count(args.buf)
+ if mark[1] > 0 and mark[1] <= count then
+ vim.cmd('normal! g`"zz')
+ end
+ end,
+})
+
+-- Close on `q` or `<Esc>`.
+autocmd({ "FileType" }, {
+ pattern = {
+ "help",
+ "qf",
+ "lspinfo",
+ "man",
+ "checkhealth",
+ "lazy",
+ },
+ command = [[
+ nnoremap <buffer><silent> q :close<CR>
+ nnoremap <buffer><silent> <Esc> :close<CR>
+ set nobuflisted
+ ]]
+})
+
+-- Autocreate a directory when saving a file.
+autocmd({ "BufWritePre" }, {
+ 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,
+})