nvim/lua/my_autocommands.lua

109 lines
3.3 KiB
Lua

local api = vim.api
-- Highlight on yank
local yankGrp = api.nvim_create_augroup('YankHighlight', { clear = true })
api.nvim_create_autocmd('TextYankPost', {
command = 'silent! lua vim.highlight.on_yank()',
group = yankGrp,
})
-- Filetypes
local fileGrp = api.nvim_create_augroup('file_type', { clear = true })
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
pattern = { '*.simvis', '*.manifest' },
callback = function()
vim.bo.filetype = 'xml'
end,
group = fileGrp,
})
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
pattern = { '*.simcfg', '*.simcon', '*.simudex' },
callback = function()
vim.bo.filetype = 'xml'
end,
group = fileGrp,
})
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
pattern = { 'JenkinsFile*' },
callback = function()
vim.bo.filetype = 'groovy'
end,
group = fileGrp,
})
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
pattern = { 'SConstruct', 'SConscript' },
callback = function()
vim.bo.filetype = 'python'
end,
group = fileGrp,
})
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
pattern = { 'doskey' },
callback = function()
vim.bo.filetype = 'dosini'
end,
group = fileGrp,
})
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
pattern = { '.clangd', '.clang-tidy' },
callback = function()
vim.bo.filetype = 'yaml'
end,
group = fileGrp,
})
-- Read and write shada file
api.nvim_create_autocmd({ 'FocusLost' }, { command = [[wshada]] })
-- go to last loc when opening a buffer
-- api.nvim_create_autocmd(
-- 'BufReadPost',
-- { command = [[if line("'\"") > 1 && line("'\"") <= line("$") | execute "normal! g`\"" | endif]] }
-- )
-- When editing a file, always jump to the last known cursor position.
-- Don't do it when the position is invalid, when inside an event handler
-- (happens when dropping a file on gvim) and for a commit message (it's
-- likely a different one than last time).
local userGrp = api.nvim_create_augroup('UserGroup', { clear = true })
vim.api.nvim_create_autocmd('BufReadPost', {
group = userGrp,
callback = function(args)
local valid_line = vim.fn.line([['"]]) >= 1 and vim.fn.line([['"]]) < vim.fn.line('$')
local not_commit = vim.b[args.buf].filetype ~= 'commit'
if valid_line and not_commit then
vim.cmd([[normal! g`"]])
end
end,
})
-- Check if we need to reload the file when it changed
api.nvim_create_autocmd({ 'FocusGained', 'BufEnter' },
{ command = [[if !bufexists("[Command Line]") | checktime | endif]] })
-- windows to close with "q"
api.nvim_create_autocmd(
'FileType',
{ pattern = { 'help', 'startuptime', 'qf', 'lspinfo' }, command = [[nnoremap <buffer><silent> q :close<CR>]] }
)
api.nvim_create_autocmd('FileType', { pattern = 'man', command = [[nnoremap <buffer><silent> q :quit<CR>]] })
-- don't auto comment new line
api.nvim_create_autocmd('BufEnter', { command = [[set formatoptions-=cro]] })
-- Keep window ratio when resize
api.nvim_create_autocmd('VimResized', { command = [[wincmd =]] })
vim.api.nvim_create_autocmd({ 'UIEnter' }, {
callback = function(event)
local client = vim.api.nvim_get_chan_info(vim.v.event.chan).client
if client ~= nil and client.name == 'Firenvim' then
vim.o.laststatus = 0
end
end
})
vim.api.nvim_create_autocmd({ 'FileType' }, { pattern = { 'norg', 'markdown' }, command = 'set conceallevel=3' })