Files
dotfiles/deprecated/nvim/custom/utils/autocmd.lua

101 lines
2.9 KiB
Lua

local autocmd = vim.api.nvim_create_autocmd
-- set markdown highlight for mdx file
autocmd({ "BufNewFile", "BufRead" }, {
pattern = { "*.mdx" },
callback = function()
local buf = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
end,
})
-- Disbale diagnostic for files in node_modules
autocmd({ "BufNewFile", "BufRead" }, {
pattern = { "**/node_modules/**", "node_modules", "/node_modules/*" },
callback = function()
vim.diagnostic.disable(0)
end,
})
autocmd("FileType", {
desc = "Close NvimTree before quit nvim",
pattern = { "NvimTree" },
callback = function(args)
autocmd("VimLeavePre", {
callback = function()
vim.api.nvim_buf_delete(args.buf, { force = true })
return true
end,
})
end,
})
autocmd("BufEnter", {
desc = "Close nvim if NvimTree is only running buffer",
command = [[if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif]],
})
-- Automatically update changed file in Vim
-- Triger `autoread` when files changes on disk
-- https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
-- https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
-- autocmd({ "FocusGained", "BufEnter", "CursorHold", "CursorHoldI" }, {
-- command = [[silent! if mode() != 'c' && !bufexists("[Command Line]") | checktime | endif]],
-- })
-- Notification after file change
-- https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
autocmd("FileChangedShellPost", {
command = [[echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None]],
})
---- 用o换行不要延续注释
local myAutoGroup = vim.api.nvim_create_augroup("myAutoGroup", {
clear = true,
})
autocmd("BufEnter", {
group = myAutoGroup,
pattern = "*",
callback = function()
vim.opt.formatoptions = vim.opt.formatoptions
- "o" -- O and o, don't continue comments
+ "r" -- But do continue when pressing enter.
end,
})
-- Highlight on yank
local highlight_group = vim.api.nvim_create_augroup("YankHighlight", { clear = true })
autocmd("TextYankPost", {
callback = function()
vim.highlight.on_yank()
end,
group = highlight_group,
pattern = "*",
})
-- Remove all trailing whitespace on save
-- autocmd("BufWritePre", {
-- command = [[:%s/\s\+$//e]],
-- group = augroup("TrimWhiteSpaceGrp", { clear = true }),
--})
-- conform.nvim auto save
-- vim.api.nvim_create_autocmd("BufWritePre", {
-- pattern = "*",
-- callback = function(args)
-- require("conform").format { bufnr = args.buf }
-- end,
-- })
-- nvim-lint
-- require('lint').linters_by_ft = {
-- javascript = {"eslint"},
-- typescript = {"eslint"},
-- }
--
-- vim.api.nvim_create_autocmd({ "BufWritePost" }, {
-- callback = function()
-- require("lint").try_lint()
-- end,
-- })