lua/user/autocommands.lua file defines autocommands that automatically execute actions in response to specific events in Neovim.
Overview
Autocommands (autocmds) allow you to automatically trigger commands when certain events occur, such as opening a file, changing buffers, or resizing windows.Configuration Structure
All autocommands are defined using Vim script wrapped in Lua:lua/user/autocommands.lua
autocmd! clears all previous autocommands in the group, preventing duplicates when sourcing the file multiple times.General Settings
Quick Close Windows
Details
Details
Event:
FileTypePattern: qf,help,man,lspinfoAction: Map q to close the windowPurpose: Allows quick closing of special windows (quickfix list, help, man pages, LSP info) by pressing q.The <buffer> option ensures the mapping only applies to that specific buffer type.Yank Highlighting
Visual Feedback
Event:
TextYankPostPattern: * (all files)Action: Briefly highlight yanked (copied) textPurpose: Provides visual feedback when you yank text, making it clear what was copied.- Uses the
Visualhighlight group - Highlight lasts 200 milliseconds
Format Options
BufWinEnter
Pattern: * (all files)
Action: Remove c, r, and o from format options
What it does:
c: Don’t auto-wrap comments using textwidthr: Don’t insert comment leader after pressing Enter in insert modeo: Don’t insert comment leader after pressingoorOin normal mode
Quickfix List Settings
Git Settings
Word Wrapping
Word Wrapping
Event:
FileType gitcommitAction: setlocal wrapPurpose: Enables line wrapping in git commit messages, making it easier to write longer commit descriptions.The local in setlocal means this only affects git commit buffers.Spell Checking
Spell Checking
Event:
FileType gitcommitAction: setlocal spellPurpose: Enables spell checking for git commit messages to catch typos.Misspelled words will be underlined. Use z= on a misspelled word to see suggestions.Markdown Settings
Enable Wrapping
Makes long lines in Markdown files wrap to the next line for better readability.
Enable Spell Check
Automatically enables spell checking for Markdown documents.
markdown
Settings:
- Line wrapping for long paragraphs
- Spell checking for writing quality
.md).
Window Resize
VimResized
Pattern: * (all situations)
Action: Equalize all window sizes
How it works:
tabdo: Execute the following command in all tabswincmd =: Equalize the size of all windows
Alpha Dashboard Settings
Dashboard Appearance
Dashboard Appearance
Event:
User AlphaReadyAction: Hide the tab line when Alpha dashboard is shownCleanup: Restore tab line when leaving the dashboardPurpose: Provides a cleaner look for the startup dashboard by hiding the tab line, then restoring it when you open a file.showtabline=0: Hide tab lineshowtabline=2: Always show tab line
Commented Autoformat
This autocommand is commented out by default. Uncomment it to enable automatic code formatting on save.
- Event:
BufWritePre(before saving) - Action: Format the buffer using LSP
- Pattern:
*(all files)
- Remove the comment markers (
--) - Save and reload the configuration
- Code will auto-format whenever you save a file
Creating Custom Autocommands
Choose the event
Determine what event should trigger your autocommand.Common events:
BufRead,BufNewFile: When opening filesBufWritePre,BufWritePost: Before/after savingFileType: When file type is detectedInsertEnter,InsertLeave: Entering/leaving insert mode
Example Custom Autocommands
Common Autocommand Events
| Event | Description |
|---|---|
BufNewFile | Starting to edit a file that doesn’t exist |
BufRead, BufReadPost | After reading a file |
BufWrite, BufWritePre | Before/during writing a file |
BufEnter | After entering a buffer |
BufLeave | Before leaving a buffer |
FileType | When file type is set |
InsertEnter | Starting insert mode |
InsertLeave | Leaving insert mode |
TextChanged | After a change was made in normal mode |
TextChangedI | After a change was made in insert mode |
VimResized | After Vim window was resized |
CursorHold | Cursor hasn’t moved for updatetime ms |
Lua-Style Autocommands
Best Practices
Use Augroups
Always wrap autocommands in augroups with
autocmd! to prevent duplicates.Be Specific
Use specific patterns instead of
* when possible to avoid unnecessary triggers.Test Carefully
Some autocommands can impact performance if they run too frequently.
Use Silent
Add
silent! to suppress error messages for non-critical commands.See Also
Options
Configure editor settings and behavior
Keymaps
Set up custom key bindings

