Important
This is now a native feature of CodeCompanion known as "Memory". Therefore, this extension is now archived. I recommend you uninstall it and use the native functionality instead.
Neovim plugin to automatically detect and add agent rules to your CodeCompanion chat context.
@arnm graciously shared this extension with the community, so all credit goes to them. From the original discussion post:
Every project has its own unique set of rules and guidelines—often scattered across different files. Remembering which files to include in your AI chat is a real cognitive burden. This extension solves that by automatically detecting and attaching all relevant rule files (like
.rules,AGENT.md, and more) to your chat context. No more manual selection or forgetting important context!It works just like other agentic tools you know and love (Cursor, ClaudeCode, etc.):
- Keeps your AI up-to-date with the latest project rules.
- Removes the overhead of managing context files yourself.
- Runs quietly in the background, always keeping your chat in sync.
Add rules in two main ways. In both cases, the rules file for the paths extracted are found and added:
- When you add a file/buffer reference
- By default, when you do @files operations (edit, create, read). You can define you own extract from message function
I literally just copy-pasta'd 🍜 it and used it to learn how to wire-up CodeCompanion Extensions. And yes, this README was AI generated for the most part. I added this bit and tweaked some stuff.
- Automatic Detection: Finds rule files (like
.rules,AGENT.md,CLAUDE.md, etc.) in your project - Path-Based Context: When you reference a file, the extension finds all relevant rule files in that path's hierarchy
- Command Integration: Works automatically with
/bufferand/filecommands - Tool Integration: Tracks references in AI tool outputs (for file reads, edits, and creation)
- Configurable: Customize which files are considered "rules"
- Toggle Controls: Enable/disable the extension as needed
By default, the extension recognizes these rule files:
.ai/rules.md.clinerules.codecompanionrules.cursorrules.github/copilot-instructions.md.goosehints.rules.windsurfrulesAGENT.mdAGENTS.mdCLAUDE.md
{
"olimorris/codecompanion.nvim",
dependencies = {
-- other dependencies...
"minusfive/codecompanion-agent-rules", -- replace with your actual repo
},
opts = {
-- other codecompanion options...
extensions = {
agent_rules = {
enabled = true,
opts = {
-- Optional: override defaults
rules_filenames = {
".rules",
"AGENT.md",
-- Add your custom rule filenames here
},
debug = false,
}
}
}
}
}- Clone this repository to your Neovim plugins directory
- Add the extension to your CodeCompanion config
require("codecompanion").setup({
extensions = {
agent_rules = {
enabled = true,
opts = {
-- Optional: override defaults
rules_filenames = {
".rules",
"AGENT.md",
-- Add your custom rule filenames here
},
debug = false,
}
}
}
})The extension provides these configuration options:
agent_rules = {
opts = {
-- List of filenames to look for as rule files
rules_filenames = {
".ai/rules.md",
".clinerules",
".codecompanionrules",
".cursorrules",
".github/copilot-instructions.md",
".goosehints",
".rules",
".windsurfrules",
"AGENT.md",
"AGENTS.md",
"CLAUDE.md",
},
-- Enable debug logging
debug = false,
-- Enable/disable the extension
enabled = true,
-- Optional: Custom function to extract file paths from messages
-- If not provided, default patterns will be used
extract_file_paths_from_chat_message = nil,
}
}The extension provides these commands:
:CodeCompanionRulesProcess- Re-evaluate rule references now:CodeCompanionRulesDebug- Toggle debug logging:CodeCompanionRulesEnable- Enable the extension:CodeCompanionRulesDisable- Disable the extension
-
When you reference a file in your chat (via paste,
/file,/buffer, etc.), the extension:- Captures the file path
- Searches for rule files in that file's directory and all parent directories up to your project root
- Automatically adds relevant rule files to your chat context
-
When you use AI tools that read, modify, or create files, the extension automatically captures those paths and includes relevant rules.
-
Rules are added as "pinned" references, so they remain in context throughout your chat session.
If the extension isn't working as expected:
- Enable debug mode with
:CodeCompanionRulesDebug - Check the logs for insights into what's happening
- Try manually triggering with
:CodeCompanionRulesProcess - Make sure your rule files match the configured filenames
- CodeCompanion - The main plugin this extends
- Original discussion thread by arnm
- Inspired by agentic code tools like Cursor, ClaudeCode, etc.