autocmd.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. -- apply default terminal settings
  2. vim.api.nvim_create_autocmd('TermOpen', {
  3. callback = function()
  4. vim.bo.scrollback = 10000
  5. vim.opt_local.number = false
  6. vim.opt_local.relativenumber = false
  7. vim.b.miniindentscope_disable = true
  8. end
  9. })
  10. -- preserve window structure when exiting terminal via C-d
  11. vim.api.nvim_create_autocmd('TermClose', {
  12. callback = function(opts)
  13. -- don't trigger when force deleting
  14. if vim.api.nvim_buf_get_option(opts.buf, 'modified') then
  15. return
  16. end
  17. MiniBufremove.delete(opts.buf)
  18. end,
  19. -- needed so statusline properly updates
  20. nested = true,
  21. })
  22. -- automatically enter/leave terminal mode
  23. vim.api.nvim_create_autocmd('TermOpen', { command = 'startinsert' })
  24. vim.api.nvim_create_autocmd({'WinEnter','BufWinEnter'}, {
  25. pattern = 'term://*',
  26. command = 'startinsert',
  27. })
  28. vim.api.nvim_create_autocmd('BufLeave', {
  29. pattern = 'term://*',
  30. command = 'stopinsert',
  31. })
  32. -- formatting
  33. vim.api.nvim_create_autocmd('BufWritePre', {
  34. callback = function(opts)
  35. if vim.bo.filetype == 'diff' then
  36. return
  37. end
  38. if not vim.g.no_lsp_format then
  39. -- check if can LSP format
  40. local clients = vim.lsp.get_active_clients({ bufnr = opts.buf })
  41. for _, client in ipairs(clients) do
  42. if client.server_capabilities.documentFormattingProvider then
  43. vim.lsp.buf.format()
  44. return
  45. end
  46. end
  47. end
  48. -- otherwise strip trailing whitespace
  49. vim.cmd('%s/\\s\\+$//e')
  50. end,
  51. })
  52. -- filetype specific options
  53. vim.api.nvim_create_autocmd('FileType', {
  54. pattern = 'markdown',
  55. callback = function()
  56. vim.bo.textwidth = 80
  57. end,
  58. })
  59. vim.api.nvim_create_autocmd('FileType', {
  60. pattern = 'http',
  61. callback = function(opts)
  62. vim.keymap.set('n', '<Leader>r', '<Plug>RestNvim', {
  63. buffer = opts.buf,
  64. })
  65. end,
  66. })