autocmds.lua
1 -- Autocmds are automatically loaded on the VeryLazy event 2 -- Default autocmds that are always set: 3 -- https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua 4 -- Add any additional autocmds here 5 6 -- [Vim/Preferences] Default color for margin 7 vim.cmd([[highlight ColorColumn guibg=#FF8C00]]) 8 9 -- [Vim/ColorScheme] SonokaiCustom 10 vim.cmd([[ 11 fu! s:sonokai_custom() abort 12 hi CursorLineNr guibg=#242424 13 hi Directory guifg=White 14 hi IndentBlanklineChar guifg=#444444 15 hi MatchParen guifg=red guibg=none gui=underline,bold 16 hi Pmenu ctermfg=81 ctermbg=16 guifg=#66D9EF guibg=#202020 17 hi PmenuSel guifg=#000000 18 hi Whitespace guifg=red " gui=underline 19 hi! PmenuThumb guibg=#66D9EF 20 hi! TSVariableBuiltin gui=bold 21 hi! link PmenuSbar Pmenu 22 hi! link TSConstant Purple 23 hi! link TSInclude Green 24 hi! link TSParameter Orange 25 hi! link TSTag Blue 26 " hi! link TSType Red 27 " hi! link TSVariableBuiltin Red 28 " hi LineNr guibg=#202020 29 " hi SignColumn guibg=#202020 30 " hi ColorColumn guibg=#111111 31 32 exec 'hi! TSVariableBuiltin gui=bold guifg=' .. synIDattr(hlID('Purple'), 'fg') 33 exec 'hi! TSInclude gui=bold guifg=' .. synIDattr(hlID('Green'), 'fg') 34 exec 'hi! TSConstant gui=bold guifg=' .. synIDattr(hlID('Purple'), 'fg') 35 endf 36 augroup SonokaiCustom 37 autocmd! 38 autocmd ColorScheme sonokai call s:sonokai_custom() 39 augroup END 40 ]]) 41 42 -- [Vim/Preferences] Go to last loc when opening a buffer. 43 vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 44 pattern = '*.nvim', 45 callback = function(event) 46 vim.bo[event.buf].filetype = 'vim' 47 end, 48 }) 49 50 -- this is a slight modification of LazyVim's default AutoCmd 51 vim.api.nvim_create_autocmd('BufReadPost', { 52 group = vim.api.nvim_create_augroup('last_loc', { clear = true }), 53 callback = function(event) 54 local exclude = { 'gitcommit' } 55 local buf = event.buf 56 if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].lazyvim_last_loc then 57 return 58 end 59 vim.b[buf].lazyvim_last_loc = true 60 -- local mark = vim.api.nvim_buf_get_mark(0, '"') 61 -- local lcount = vim.api.nvim_buf_line_count(0) 62 local mark = vim.api.nvim_buf_get_mark(buf, '"') 63 local lcount = vim.api.nvim_buf_line_count(buf) 64 if mark[1] > 0 and mark[1] <= lcount then 65 -- Protected call to catch errors. 66 pcall(vim.api.nvim_win_set_cursor, 0, mark) 67 end 68 end, 69 }) 70 71 -- [Vim/LSP] Buffer keymaps and options when LSP attaches 72 vim.api.nvim_create_autocmd('LspAttach', { 73 group = vim.api.nvim_create_augroup('user_lsp_attach', { clear = true }), 74 callback = function(args) 75 local bufnr = args.buf 76 local client = vim.lsp.get_client_by_id(args.data.client_id) 77 if client and client.supports_method(client, 'textDocument/implementation') then 78 vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr, desc = 'LSP hover' }) 79 vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { 80 buffer = bufnr, 81 desc = 'Go to definition', 82 }) 83 if client and client.supports_method(client, 'textDocument/completion') then 84 vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc' 85 end 86 if client and client.supports_method(client, 'textDocument/definition') then 87 vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc' 88 end 89 end 90 end, 91 }) 92 93 -- [Golang] Set filetype for Proto files 94 vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 95 pattern = { 'proto' }, 96 callback = function() 97 vim.opt_local.filetype = 'proto' 98 end, 99 }) 100 101 -- [ProtoBuf] Handler for protofiles: start proto LSP 102 vim.api.nvim_create_autocmd('FileType', { 103 pattern = 'proto', 104 callback = function(args) 105 vim.lsp.start({ 106 name = 'proto', 107 root_dir = vim.fs.root(args.buf, { '*.proto' }), 108 cmd = { 'protols' }, 109 }) 110 end, 111 }) 112 113 -- [ProtoBuf] When proto LSP attaches, detach clangd from this buffer to avoid conflicts 114 vim.api.nvim_create_autocmd('LspAttach', { 115 group = vim.api.nvim_create_augroup('proto_detach_clangd', { clear = true }), 116 callback = function(args) 117 local client = vim.lsp.get_client_by_id(args.data.client_id) 118 if not client or client.name ~= 'proto' then 119 return 120 end 121 local bufnr = args.buf 122 for _, c in ipairs(vim.lsp.get_clients({ bufnr = bufnr })) do 123 if c.name == 'clangd' then 124 vim.lsp.buf_detach_client(bufnr, c.id) 125 end 126 end 127 end, 128 }) 129 130 -- [Terraform] Handler for language server 131 -- Terraform filetype and settings only (no manual LSP/format) 132 vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, { 133 pattern = { '*.tf', '*.hcl', '*.tfvars' }, 134 group = vim.api.nvim_create_augroup('FixTerraformCommentString', { clear = true }), 135 callback = function(args) 136 vim.opt_local.filetype = 'terraform' 137 vim.bo[args.buf].commentstring = '# %s' 138 end, 139 }) 140 141 -- [Tilt] Handler for Tiltfiles 142 vim.api.nvim_create_autocmd('FileType', { 143 pattern = 'bzl', 144 callback = function(ev) 145 vim.lsp.start({ 146 name = 'bzl', 147 root_dir = vim.fs.root(ev.buf, { 'Tiltfile', 'bzl' }), 148 codelens = { enable = true }, 149 cmd = { 'tilt', 'lsp', 'start' }, 150 docs = { 151 description = [[ 152 https://docs.stack.build/docs/cli/installation 153 154 https://docs.stack.build/docs/vscode/starlark-language-server 155 ]], 156 }, 157 }) 158 end, 159 }) 160 161 -- [Golang] Set filetype for Go template files 162 vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 163 pattern = { '*.gotmpl', '*.go.tmpl', '*.tmpl' }, 164 callback = function() 165 vim.opt_local.filetype = 'gotexttmpl' 166 end, 167 }) 168 169 -- [Kustomize] Set filetype for kustomization files 170 vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 171 pattern = { 'kustomization.yaml' }, 172 callback = function() 173 vim.opt_local.filetype = 'yaml' 174 end, 175 }) 176 177 -- [Golang] Set filetype for Go HTML template files 178 vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 179 pattern = { '*.gohtml', '*.go.html' }, 180 callback = function() 181 vim.opt_local.filetype = 'html' 182 end, 183 }) 184 185 -- [Spell] Wrap and check for spell in text file types. 186 vim.api.nvim_create_autocmd('FileType', { 187 group = vim.api.nvim_create_augroup('wrap_spell', { clear = true }), 188 pattern = { 'gitcommit', 'markdown' }, 189 callback = function() 190 vim.opt_local.wrap = true 191 vim.opt_local.spell = true 192 end, 193 }) 194 195 -- [User Commands] 196 vim.api.nvim_create_user_command('InsertDate', 'r!date "+\\%Y-\\%m-\\%d"', {}) 197 198 vim.api.nvim_create_augroup('LspStopByFormat', { clear = true }) 199 vim.api.nvim_create_autocmd({ 'FileType' }, { 200 pattern = { 'kustomization.yaml' }, 201 command = 'LspStop', 202 group = 'LspStopByFormat', 203 })