absvim/lua/autocmds.lua
2023-02-27 18:27:44 -08:00

160 lines
3.3 KiB
Lua

local autocmd = vim.api.nvim_create_autocmd -- Create autocommand
---- Toggle between line numbers and relative line numbers
local _group = vim.api.nvim_create_augroup("LineNumber", { clear = true })
local function relativeln(target)
if vim.b.lnstatus == nil then
vim.b.lnstatus = "number"
end
if vim.b.lnstatus ~= "nonumber" then
if target == "number" then
vim.o.number = true
vim.o.relativenumber = false
else
vim.o.number = true
vim.o.relativenumber = true
end
else
vim.o.number = false
end
end
autocmd("InsertEnter", {
pattern = "*",
callback = function()
relativeln("number")
end,
once = false,
group = _group,
})
autocmd("InsertLeave", {
pattern = "*",
callback = function()
relativeln("relativenumber")
end,
once = false,
group = _group,
})
autocmd("FocusLost", {
pattern = "*",
callback = function()
relativeln("number")
end,
once = false,
group = _group,
})
autocmd("CursorMoved", {
pattern = "*",
callback = function()
relativeln("relativenumber")
end,
once = false,
group = _group,
})
function Toggleln()
if vim.b.lnstatus == nil then
vim.b.lnstatus = "number"
end
if vim.b.lnstatus == "number" then
vim.o.number = false
vim.o.relativenumber = false
vim.b.lnstatus = "nonumber"
else
vim.o.number = true
vim.o.relativenumber = true
vim.b.lnstatus = "number"
end
end
---- trim whitespace on save
autocmd("BufWritePre", {
pattern = "*",
callback = function()
vim.cmd([[:%s/\s\+$//e]])
end,
once = false,
group = _group,
})
-- change tmux title
autocmd("BufReadPost", {
pattern = "*",
callback = function()
vim.cmd([[call system("tmux rename-window '" . expand("%:t") . "'")]])
end,
once = false,
group = _group,
})
autocmd("FileReadPost", {
pattern = "*",
callback = function()
vim.cmd([[call system("tmux rename-window '" . expand("%:t") . "'")]])
end,
once = false,
group = _group,
})
autocmd("BufNewFile", {
pattern = "*",
callback = function()
vim.cmd([[call system("tmux rename-window '" . expand("%:t") . "'")]])
end,
once = false,
group = _group,
})
autocmd("BufEnter", {
pattern = "*",
callback = function()
vim.cmd([[call system("tmux rename-window '" . expand("%:t") . "'")]])
end,
once = false,
group = _group,
})
-- auto close nvimtree
autocmd('BufEnter', {
command = "if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif",
nested = true,
})
-- dont show line numbers for nvimtree buffers
autocmd("BufEnter",
{
pattern = { "NvimTree*" },
callback = function()
vim.b.lnstatus = "nonumber"
end
})
-- Disable the statusline, tabline and cmdline while the alpha dashboard is open
autocmd('User', {
pattern = 'AlphaReady',
desc = 'disable status, tabline and cmdline for alpha',
callback = function()
vim.go.laststatus = 1
vim.opt.showtabline = 1
vim.opt.cmdheight = 1
end,
})
autocmd('BufUnload', {
buffer = 0,
desc = 'enable status, tabline and cmdline after alpha',
callback = function()
vim.go.laststatus = 2
vim.opt.showtabline = 2
vim.opt.cmdheight = 1
end,
})
-- auto format on write
vim.cmd [[autocmd BufWritePre * lua vim.lsp.buf.format()]]
-- syntax fix
vim.cmd("syntax sync fromstart")