autocmd.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. local myAutoGroup = vim.api.nvim_create_augroup("myAutoGroup", {
  2. clear = true,
  3. })
  4. local autocmd = vim.api.nvim_create_autocmd
  5. -- 自动切换输入法,需要安装 im-select
  6. -- https://github.com/daipeihust/im-select
  7. -- autocmd("InsertLeave", {
  8. -- group = myAutoGroup,
  9. -- callback = require("utils.im-select").insertLeave,
  10. -- })
  11. -- autocmd("InsertEnter", {
  12. -- group = myAutoGroup,
  13. -- callback = require("utils.im-select").insertEnter,
  14. -- })
  15. -- 进入Terminal 自动进入插入模式
  16. autocmd("TermOpen", {
  17. group = myAutoGroup,
  18. command = "startinsert",
  19. })
  20. -- 保存时自动格式化
  21. autocmd("BufWritePre", {
  22. group = myAutoGroup,
  23. pattern = { "*.lua", "*.py", "*.sh" },
  24. callback = function()
  25. vim.lsp.buf.format()
  26. end,
  27. })
  28. -- 修改lua/plugins.lua 自动更新插件
  29. autocmd("BufWritePost", {
  30. group = myAutoGroup,
  31. -- autocmd BufWritePost plugins.lua source <afile> | PackerSync
  32. callback = function()
  33. if vim.fn.expand("<afile>") == "lua/plugins.lua" then
  34. vim.api.nvim_command("source lua/plugins.lua")
  35. vim.api.nvim_command("PackerSync")
  36. end
  37. end,
  38. })
  39. -- Highlight on yank
  40. autocmd("TextYankPost", {
  41. callback = function()
  42. vim.highlight.on_yank()
  43. end,
  44. group = myAutoGroup,
  45. pattern = "*",
  46. })
  47. -- 用o换行不要延续注释
  48. autocmd("BufEnter", {
  49. group = myAutoGroup,
  50. pattern = "*",
  51. callback = function()
  52. vim.opt.formatoptions = vim.opt.formatoptions
  53. - "o" -- O and o, don't continue comments
  54. + "r" -- But do continue when pressing enter.
  55. end,
  56. })
  57. -- 保存Fold
  58. local saveable_type = { "*.lua", "*.js", "*.jsx", "*.ts", "*.tsx" }
  59. autocmd("BufWinEnter", {
  60. group = myAutoGroup,
  61. pattern = saveable_type,
  62. command = "silent! loadview",
  63. })
  64. autocmd("BufWrite", {
  65. group = myAutoGroup,
  66. pattern = saveable_type,
  67. command = "mkview",
  68. })