]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/config/autocmds.lua
Merge branch 'remove-lazyvim'
[dotfiles.git] / nvim / lua / config / autocmds.lua
1 -- Autocmds are automatically loaded by config.init
2
3 local function augroup(name)
4 return vim.api.nvim_create_augroup("rmz_" .. name, { clear = true })
5 end
6
7 -- Check if we need to reload the file when it changed
8 vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
9 group = augroup("checktime"),
10 callback = function()
11 if vim.o.buftype ~= "nofile" then
12 vim.cmd("checktime")
13 end
14 end,
15 })
16
17 -- Highlight on yank
18 vim.api.nvim_create_autocmd("TextYankPost", {
19 group = augroup("highlight_yank"),
20 callback = function()
21 (vim.hl or vim.highlight).on_yank()
22 end,
23 })
24
25 -- resize splits if window got resized
26 vim.api.nvim_create_autocmd({ "VimResized" }, {
27 group = augroup("resize_splits"),
28 callback = function()
29 local current_tab = vim.fn.tabpagenr()
30 vim.cmd("tabdo wincmd =")
31 vim.cmd("tabnext " .. current_tab)
32 end,
33 })
34
35 -- go to last loc when opening a buffer
36 vim.api.nvim_create_autocmd("BufReadPost", {
37 group = augroup("last_loc"),
38 callback = function(event)
39 local exclude = { "gitcommit" }
40 local buf = event.buf
41 if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].rmz_last_loc then
42 return
43 end
44 vim.b[buf].rmz_last_loc = true
45 local mark = vim.api.nvim_buf_get_mark(buf, '"')
46 local lcount = vim.api.nvim_buf_line_count(buf)
47 if mark[1] > 0 and mark[1] <= lcount then
48 pcall(vim.api.nvim_win_set_cursor, 0, mark)
49 end
50 end,
51 })
52
53 -- close some filetypes with <q>
54 vim.api.nvim_create_autocmd("FileType", {
55 group = augroup("close_with_q"),
56 pattern = {
57 "checkhealth",
58 "dbout",
59 "gitsigns-blame",
60 "help",
61 "lspinfo",
62 "notify",
63 "qf",
64 "spectre_panel",
65 "startuptime",
66 "tsplayground",
67 },
68 callback = function(event)
69 vim.bo[event.buf].buflisted = false
70 vim.schedule(function()
71 vim.keymap.set("n", "q", function()
72 vim.cmd("close")
73 pcall(vim.api.nvim_buf_delete, event.buf, { force = true })
74 end, {
75 buffer = event.buf,
76 silent = true,
77 desc = "Quit buffer",
78 })
79 end)
80 end,
81 })
82
83 -- make it easier to close man-files when opened inline
84 vim.api.nvim_create_autocmd("FileType", {
85 group = augroup("man_unlisted"),
86 pattern = { "man" },
87 callback = function(event)
88 vim.bo[event.buf].buflisted = false
89 end,
90 })
91
92 -- Fix conceallevel for json files
93 vim.api.nvim_create_autocmd({ "FileType" }, {
94 group = augroup("json_conceal"),
95 pattern = { "json", "jsonc", "json5" },
96 callback = function()
97 vim.opt_local.conceallevel = 0
98 end,
99 })