aboutsummaryrefslogtreecommitdiff
path: root/.config/nvim/lua/custom-autocmd.lua
diff options
context:
space:
mode:
authorMichal Hanus <mikehanus@protonmail.com>2025-12-29 19:02:55 +0100
committerMichal Hanus <mikehanus@protonmail.com>2025-12-29 19:02:55 +0100
commit5eda7770be54f222e27e87190a95847e6d0e2c8c (patch)
tree6974ac951d1f955f1a05144f1504a41fcf990251 /.config/nvim/lua/custom-autocmd.lua
parent688a636a43e70da5337c8dd671d4269621fae80b (diff)
nvim setup
Diffstat (limited to '.config/nvim/lua/custom-autocmd.lua')
-rw-r--r--.config/nvim/lua/custom-autocmd.lua105
1 files changed, 105 insertions, 0 deletions
diff --git a/.config/nvim/lua/custom-autocmd.lua b/.config/nvim/lua/custom-autocmd.lua
new file mode 100644
index 0000000..21fa15c
--- /dev/null
+++ b/.config/nvim/lua/custom-autocmd.lua
@@ -0,0 +1,105 @@
+local fn = vim.fn
+local api = vim.api
+
+local utils = require("utils")
+
+-- Display a message when the current file is not in utf-8 format.
+-- Note that we need to use `unsilent` command here because of this issue:
+-- https://github.com/vim/vim/issues/4379
+api.nvim_create_autocmd({ "BufRead" }, {
+ pattern = "*",
+ group = api.nvim_create_augroup("non_utf8_file", { clear = true }),
+ callback = function()
+ if vim.bo.fileencoding ~= "utf-8" then
+ vim.notify("File not in UTF-8 format!", vim.log.levels.WARN, { title = "nvim-config" })
+ end
+ end,
+})
+
+-- highlight yanked region, see `:h lua-highlight`
+local yank_group = api.nvim_create_augroup("highlight_yank", { clear = true })
+api.nvim_create_autocmd({ "TextYankPost" }, {
+ pattern = "*",
+ group = yank_group,
+ callback = function()
+ vim.highlight.on_yank { higroup = "YankColor", timeout = 300 }
+ end,
+})
+
+api.nvim_create_autocmd({ "CursorMoved" }, {
+ pattern = "*",
+ group = yank_group,
+ callback = function()
+ vim.g.current_cursor_pos = vim.fn.getcurpos()
+ end,
+})
+
+api.nvim_create_autocmd("TextYankPost", {
+ pattern = "*",
+ group = yank_group,
+ callback = function(ev)
+ if vim.v.event.operator == 'y' then
+ vim.fn.setpos('.', vim.g.current_cursor_pos)
+ end
+ end,
+})
+
+-- Auto-create dir when saving a file, in case some intermediate directory does not exist
+api.nvim_create_autocmd({ "BufWritePre" }, {
+ pattern = "*",
+ group = api.nvim_create_augroup("auto_create_dir", { clear = true }),
+ callback = function(ctx)
+ local dir = fn.fnamemodify(ctx.file, ":p:h")
+ utils.may_create_dir(dir)
+ end,
+})
+
+-- Automatically reload the file if it is changed outside of Nvim, see https://unix.stackexchange.com/a/383044/221410.
+-- It seems that `checktime` does not work in command line. We need to check if we are in command
+-- line before executing this command, see also https://vi.stackexchange.com/a/20397/15292 .
+api.nvim_create_augroup("auto_read", { clear = true })
+
+api.nvim_create_autocmd({ "FileChangedShellPost" }, {
+ pattern = "*",
+ group = "auto_read",
+ callback = function()
+ vim.notify("File changed on disk. Buffer reloaded!", vim.log.levels.WARN, { title = "nvim-config" })
+ end,
+})
+
+api.nvim_create_autocmd({ "FocusGained", "CursorHold" }, {
+ pattern = "*",
+ group = "auto_read",
+ callback = function()
+ if fn.getcmdwintype() == "" then
+ vim.cmd("checktime")
+ end
+ end,
+})
+
+-- Resize all windows when we resize the terminal
+api.nvim_create_autocmd("VimResized", {
+ group = api.nvim_create_augroup("win_autoresize", { clear = true }),
+ desc = "autoresize windows on resizing operation",
+ command = "wincmd =",
+})
+
+local function open_nvim_tree(data)
+ -- check if buffer is a directory
+ local directory = vim.fn.isdirectory(data.file) == 1
+
+ if not directory then
+ return
+ end
+
+ -- create a new, empty buffer
+ vim.cmd.enew()
+
+ -- wipe the directory buffer
+ vim.cmd.bw(data.buf)
+
+ -- open the tree
+ require("nvim-tree.api").tree.open()
+end
+
+vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })