Events / Hooks
In order to enable a tighter integration between CodeCompanion and your Neovim config, the plugin fires events at various points during its lifecycle.
List of Events
The events that are fired from within the plugin are:
CodeCompanionChatCreated
- Fired after a chat has been created for the first timeCodeCompanionChatOpened
- Fired after a chat has been openedCodeCompanionChatHidden
- Fired after a chat has been hiddenCodeCompanionChatClosed
- Fired after a chat has been permanently closedCodeCompanionChatSubmitted
- Fired after a chat has been submittedCodeCompanionChatStopped
- Fired after a chat has been stoppedCodeCompanionChatCleared
- Fired after a chat has been clearedCodeCompanionChatAdapter
- Fired after the adapter has been set in the chatCodeCompanionChatModel
- Fired after the model has been set in the chatCodeCompanionChatPin
- Fired after a pinned reference has been updated in the messages tableCodeCompanionAgentStarted
- Fired when an agent has been initiated to run toolsCodeCompanionAgentFinished
- Fired when an agent has finished running all toolsCodeCompanionToolAdded
- Fired when a tool has been added to a chatCodeCompanionToolStarted
- Fired when a tool has started executingCodeCompanionToolFinished
- Fired when a tool has finished executingCodeCompanionInlineStarted
- Fired at the start of the Inline strategyCodeCompanionInlineFinished
- Fired at the end of the Inline strategyCodeCompanionRequestStarted
- Fired at the start of any API requestCodeCompanionRequestStreaming
- Fired at the start of a streaming API requestCodeCompanionRequestFinished
- Fired at the end of any API requestCodeCompanionDiffAttached
- Fired when in Diff modeCodeCompanionDiffDetached
- Fired when exiting Diff modeCodeCompanionDiffAccepted
- Fired when a user accepts a changeCodeCompanionDiffRejected
- Fired when a user rejects a change
There are also events that can be utilized to trigger commands from within the plugin:
CodeCompanionChatRefreshCache
- Used to refresh conditional elements in the chat buffer
Event Data
Each event also comes with a data payload. For example, with CodeCompanionRequestStarted
:
lua
{
buf = 10,
data = {
adapter = {
formatted_name = "Copilot",
model = "o3-mini-2025-01-31",
name = "copilot"
},
bufnr = 10,
id = 6107753,
strategy = "chat"
},
event = "User",
file = "CodeCompanionRequestStarted",
group = 14,
id = 30,
match = "CodeCompanionRequestStarted"
}
And the CodeCompanionRequestFinished
also has a data.status
value.
Consuming an Event
Events can be hooked into as follows:
lua
local group = vim.api.nvim_create_augroup("CodeCompanionHooks", {})
vim.api.nvim_create_autocmd({ "User" }, {
pattern = "CodeCompanionInline*",
group = group,
callback = function(request)
if request.match == "CodeCompanionInlineFinished" then
-- Format the buffer after the inline request has completed
require("conform").format({ bufnr = request.buf })
end
end,
})
You can trigger an event with:
lua
vim.api.nvim_exec_autocmds("User", {
pattern = "CodeCompanionChatRefreshCache",
})