diff options
-rw-r--r-- | plugin/autocmd.lua | 50 |
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, +}) |