Skip to main content
This page documents all autocommands defined in lua/core/autocmds.lua. Autocommands automatically execute actions in response to specific events.

LSP attachment guard

Prevents LSP from attaching to non-file buffers like fugitive, codediff, and other special buffer types.
Event: LspAttach
Source: lua/core/autocmds.lua:4
Purpose: Detach LSP clients from buffers that use special URI schemes (except file://).Behavior:
  • Checks the buffer name when LSP attaches
  • Allows file:// scheme and plain paths
  • Blocks all other URI schemes (like fugitive://, codediff://, etc.)
  • Schedules client detachment for non-file buffers
Implementation details:
api.nvim_create_autocmd("LspAttach", {
  callback = function(args)
    local bufname = api.nvim_buf_get_name(args.buf)
    if bufname:match("^%w+://") and not bufname:match("^file://") then
      vim.schedule(function()
        local client = vim.lsp.get_client_by_id(args.data.client_id)
        if client then
          vim.lsp.buf_detach_client(args.buf, args.data.client_id)
        end
      end)
    end
  end,
})
This prevents unnecessary LSP diagnostics and completions in special buffers that aren’t actual files.

Comment formatting

Disables automatic comment continuation on new lines.
Event: BufEnter
Source: lua/core/autocmds.lua:20
Purpose: Prevent Neovim from automatically continuing comments when pressing Enter.Behavior:
  • Removes c, r, and o flags from formatoptions
  • c: Don’t auto-wrap comments using textwidth
  • r: Don’t auto-insert comment leader after Enter in insert mode
  • o: Don’t auto-insert comment leader after ‘o’ or ‘O’ in normal mode
Command: set formatoptions-=cro
This runs every time you enter a buffer, ensuring the setting persists even if plugins or ftplugins try to override it.

Restore cursor position

Returns to the last known cursor position when reopening a file.
Event: BufReadPost
Source: lua/core/autocmds.lua:24
Purpose: Jump to the last position where you were editing when you open a file.Behavior:
  • Reads the " mark (last exit position) from the buffer
  • Validates that the position is within the current file’s line count
  • Sets cursor to that position using pcall for safety
  • Only triggers when opening existing files
Implementation details:
api.nvim_create_autocmd("BufReadPost", {
  callback = function()
    local mark = vim.api.nvim_buf_get_mark(0, '"')
    local lcount = vim.api.nvim_buf_line_count(0)
    if mark[1] > 0 and mark[1] <= lcount then
      pcall(vim.api.nvim_win_set_cursor, 0, mark)
    end
  end,
})
The pcall wrapper ensures that errors in setting cursor position don’t crash Neovim.

ESLint daemon cleanup

Stops the eslint_d daemon when exiting Neovim to prevent orphaned processes.
Event: VimLeavePre
Source: lua/core/autocmds.lua:35
Purpose: Clean up eslint_d daemon process before Neovim exits.Behavior:
  • Checks if eslint_d is installed and executable
  • Runs eslint_d stop command on Neovim exit
  • Prevents orphaned daemon processes from consuming system resources
Implementation details:
api.nvim_create_autocmd("VimLeavePre", {
  callback = function()
    if vim.fn.executable("eslint_d") == 1 then
      vim.fn.system("eslint_d stop")
    end
  end,
})
This only affects eslint_d. If you use other linter daemons, you may want to add similar cleanup logic.

Quick close for special filetypes

Allows closing certain buffer types with just the q key.
Event: FileType
Group: close_with_q
Source: lua/core/autocmds.lua:44
Purpose: Map q to close special buffers like help, quickfix, and plugin windows.Affected filetypes:
  • PlenaryTestPopup - Plenary test output
  • help - Vim help windows
  • lspinfo - LSP information window
  • man - Manual pages
  • notify - Notification windows
  • qf - Quickfix list
  • spectre_panel - Search/replace panel
  • startuptime - Startup profiling
  • tsplayground - Treesitter playground
  • neotest-output - Test output
  • checkhealth - Health check results
  • neotest-summary - Test summary
  • neotest-output-panel - Test output panel
  • codediff - Code diff viewer
Behavior:
  • Sets buffer to not listed (buflisted = false)
  • Maps q in normal mode to :close command
  • Mapping is buffer-local and silent
Implementation details:
api.nvim_create_autocmd("FileType", {
  group = api.nvim_create_augroup("close_with_q", { clear = true }),
  pattern = { "help", "lspinfo", "qf", ... },
  callback = function(event)
    vim.bo[event.buf].buflisted = false
    vim.keymap.set("n", "q", "<cmd>close<cr>", { 
      buffer = event.buf, 
      silent = true 
    })
  end,
})
The autogroup is created with clear = true to prevent duplicate autocommands if the config is reloaded.

Summary

This configuration includes 5 autocommands that handle:
  1. LSP attachment filtering - Keeps LSP focused on real files
  2. Comment formatting - Prevents annoying auto-comments
  3. Cursor position restoration - Picks up where you left off
  4. Daemon cleanup - Prevents orphaned eslint_d processes
  5. Quick window closing - Streamlines workflow with special buffers
All autocommands use Lua callbacks for better performance and maintainability compared to Vimscript commands.

Build docs developers (and LLMs) love