2024-03-05 00:40:49 +01:00

68 lines
1.5 KiB
Lua

local on = vim.api.nvim_create_autocmd
local group = vim.api.nvim_create_augroup("autocmds", {clear = true})
on({ "BufNewFile", "BufRead" }, {
desc = "Enable word wrap in text files and markdown documents",
group = group,
pattern = { "*.txt", "*.md" },
callback = function()
vim.opt_local.wrap = true
vim.opt_local.signcolumn = "no"
end,
})
on({ "TermOpen", "TermEnter" }, {
desc = "Disable sign column in terminals",
group = group,
callback = function()
vim.opt_local.signcolumn = "no"
end,
})
on({ "TextYankPost" }, {
desc = "Highlight when copying text",
group = group,
callback = function()
vim.highlight.on_yank()
end,
})
on({ "VimEnter" }, {
desc = "Allow opening a directory and jumping to project root",
group = group,
callback = function()
-- Change file to its directory if needed
local name = vim.api.nvim_buf_get_name(0)
if vim.fn.isdirectory(name) == 0 then
name = vim.fs.dirname(name)
end
-- Switch to root directory of the project
local root_patterns = { ".git", "go.mod", "init.lua" }
local root_dir = vim.fs.dirname(vim.fs.find(root_patterns, {
upward = true,
path = name,
})[1])
if root_dir then
vim.api.nvim_set_current_dir(root_dir)
else
vim.api.nvim_set_current_dir(name)
end
end,
})
on({ "VimEnter" }, {
desc = "Open file explorer if there is enough horizontal space",
group = group,
callback = function()
local files = require("nvim-tree.api")
local width = vim.go.columns
if width > 120 then
files.tree.toggle({ focus = false })
end
end,
})