|
| 1 | +--- |
| 2 | +id: autocmds |
| 3 | +title: Customize Autocommands |
| 4 | +--- |
| 5 | + |
| 6 | +A large part of configuring Neovim is the use of autocommands and autocmd groups (augroups). AstroNvim makes it easy to configure autocommands through [AstroCore](https://github.com/AstroNvim/astrocore) and [AstroLSP](https://github.com/AstroNvim/astrolsp). AstroCore handles the general autocommands that are applied on startup where AstroLSP handles autocommands that are set when a language server attaches. This page goes over how to customize these mappings to fit your needs. |
| 7 | + |
| 8 | +### Add Custom Autocommands |
| 9 | + |
| 10 | +These tables are a direct conversion to the `vim.api.nvim_create_autocmd({event}, {autocmd_opts})` and `vim.api.nvim_create_augroup({name}, {augroup_opts})` Lua APIs. The key into the table is the `{name}` field for creating an `augroup`, and the value is a list-like table where each element is the `{autocmds_opts}` table with the `{event}` in as a value with the key `"event"`. Also AstroLSP supports adding a `cond` key which can dictate when the autocmd should be created (this is described in detail in the [AstroLSP plugin configuration documentation](https://github.com/AstroNvim/astrolsp#%EF%B8%8F-configuration)) Here is a simple plugin specification example of setting both core and LSP autocmds: |
| 11 | + |
| 12 | +```lua title="lua/plugins/autocmds.lua" |
| 13 | +return { |
| 14 | + { |
| 15 | + "AstroNvim/astrocore", |
| 16 | + ---@type AstroCoreOpts |
| 17 | + opts = { |
| 18 | + autocmds = { |
| 19 | + -- first key is the augroup name |
| 20 | + terminal_settings = { |
| 21 | + -- the value is a list of autocommands to create |
| 22 | + { |
| 23 | + -- event is added here as a string or a list-like table of events |
| 24 | + event = "TermOpen", |
| 25 | + -- the rest of the autocmd options (:h nvim_create_autocmd) |
| 26 | + desc = "Disable line number/fold column/sign column for terminals", |
| 27 | + callback = function() |
| 28 | + vim.opt_local.number = false |
| 29 | + vim.opt_local.relativenumber = false |
| 30 | + vim.opt_local.foldcolumn = "0" |
| 31 | + vim.opt_local.signcolumn = "no" |
| 32 | + end, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + "AstroNvim/astrolsp", |
| 40 | + ---@type AstroLSPOpts |
| 41 | + opts = { |
| 42 | + automcds = { |
| 43 | + -- these autocommands will only be created in buffers where |
| 44 | + -- a language servers attaches |
| 45 | + lsp_codelens_refresh = { |
| 46 | + -- condition to create/delete auto command group |
| 47 | + -- can either be a string of a client capability |
| 48 | + -- or a function of `fun(client, bufnr): boolean` |
| 49 | + cond = "textDocument/codeLens", |
| 50 | + { |
| 51 | + -- events to trigger |
| 52 | + event = { "InsertLeave", "BufEnter" }, |
| 53 | + -- the rest of the autocmd options (:h nvim_create_autocmd) |
| 54 | + desc = "Refresh codelens (buffer)", |
| 55 | + callback = function(args) |
| 56 | + vim.lsp.codelens.refresh { bufnr = args.buf } |
| 57 | + end, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Removing Built-in Autocommands |
| 67 | + |
| 68 | +AstroNvim comes with a number of autocommands for improving the out of the box experience. This includes things such as mapping `q` to buffer close in non-editable buffers, disabling features for large buffers, or flashing the highlight of text on copy. These can be easily disabled similar to how mappings are disabled, by setting the `augroup` name to `false`. |
| 69 | + |
| 70 | +For example, if you want to disable the autocommand that highlights yanked text (using `vim.highlight.on_yank`) you can add the following plugin spec: |
| 71 | + |
| 72 | +```lua title="lua/plugins/disable_highlight_on_yank.lua" |
| 73 | +return { |
| 74 | + "AstroNvim/astrocore", |
| 75 | + ---@type AstroCoreOpts |
| 76 | + opts = { |
| 77 | + autocmds = { |
| 78 | + -- set a key to false to disable the autocommands from being created |
| 79 | + highlightyank = false |
| 80 | + }, |
| 81 | + }, |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +:::tip[Debugging Tip] |
| 86 | + |
| 87 | +You can use the `require("astrocore").plugin_opts()` function to check what autocommands and augroups exist easily: |
| 88 | + |
| 89 | +```lua |
| 90 | +:lua =vim.tbl_keys(require("astrocore").plugin_opts("astrocore").autocmds) |
| 91 | +``` |
| 92 | + |
| 93 | +::: |
0 commit comments