Markdown Context Generator for Code.
Select code, add annotations, and generate Markdown context for AI prompts.
- Annotate: Select code in visual mode and add comments via a floating window.
- Context Generation: Automatically formats the selected code and your comment into Markdown.
- Clipboard Integration: Copies the generated Markdown directly to your system clipboard (
+register). - Project Scope: Annotations are saved per project.
- Quickfix Review: View all annotations in the Quickfix list.
- Management Buffer: Dedicated buffer to list, open, and delete annotations.
- fzf-lua Integration: Fuzzy search annotations (requires fzf-lua).
Using lazy.nvim:
{
"gen4438/marginalia.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
config = function()
require("marginalia").setup({
-- include_code = true, -- Include code block in generated Markdown (default: false)
-- textobject = "a", -- Character for text objects/navigation: ia, aa, ]a, [a
keymaps = {
-- annotate = "<leader>ma", -- Visual + Normal mode (default)
-- list = "<leader>ml", -- Normal mode (default)
-- manager = "<leader>mm", -- Normal mode (default)
-- search = "<leader>ms", -- Normal mode (default)
},
})
end
}- Select a block of code in Visual Mode, or place your cursor on a line in Normal Mode.
- Run
:MarginaliaAnnotate(visual) or:MarginaliaAnnotateLine(normal), or use your keymapping. - A floating window will appear. Type your comment / annotation.
- Press
<CR>(Enter) in Normal mode to save.- The annotation is saved to disk.
- A Markdown snippet is generated and copied to your clipboard.
Example Output (default):
@src/main.rs#10-15
Explanation of main functionWith include_code = true:
@src/main.rs#10-15
\`\`\`rust
fn main() {
println!("Hello");
}
\`\`\`
Explanation of main functionNote: You can override the include_code setting per-invocation by using the specific commands: :MarginaliaAnnotateCode, :MarginaliaAnnotateNoCode, :MarginaliaAnnotateLineCode, or :MarginaliaAnnotateLineNoCode.
:MarginaliaList- Open the Quickfix list with all annotations.
:MarginaliaManager- Open an editable management buffer.- By default, annotations are displayed in a folded summary view (one line per annotation).
- Use standard Vim fold commands to toggle between summary and expanded (full Markdown) views.
Fold Controls:
| Key | Action |
|---|---|
za |
Toggle fold for the annotation under cursor |
zR |
Expand all annotations (show code + full comments) |
zM |
Collapse all annotations (summary view) |
Navigation & Selection:
| Key | Action |
|---|---|
]a |
Jump to next annotation (supports count: 3]a) |
[a |
Jump to previous annotation |
via |
Select inner annotation (code + comment, excluding header) |
vaa |
Select entire annotation block (including header) |
Editing:
| Key | Action |
|---|---|
dd on header line (@file#...) |
Delete the entire annotation block |
dd on comment/code line |
Delete that line only (normal editing) |
daa |
Delete entire annotation block |
<CR> |
Open the file and visually select the annotated range |
q |
Save changes and close |
:w |
Save changes without closing |
Text objects ia / aa work with all operators: dia, yaa, cia, etc.
:MarginaliaSearch- Fuzzy search annotations with fzf-lua.- Requires fzf-lua to be installed.
| Command | Description |
|---|---|
:MarginaliaAnnotate |
Annotate selected code (visual mode, uses options) |
:MarginaliaAnnotateCode |
Annotate selected code (visual mode, includes snippet) |
:MarginaliaAnnotateNoCode |
Annotate selected code (visual mode, excludes snippet) |
:MarginaliaAnnotateLine |
Annotate current line (normal mode, uses options) |
:MarginaliaAnnotateLineCode |
Annotate current line (normal mode, includes snippet) |
:MarginaliaAnnotateLineNoCode |
Annotate current line (normal mode, excludes snippet) |
:MarginaliaList |
Open quickfix list with annotations |
:MarginaliaManager |
Open annotation manager buffer |
:MarginaliaSearch |
Fuzzy search annotations (fzf-lua) |
local marginalia = require("marginalia")
marginalia.annotate() -- Annotate current visual selection
marginalia.annotate_line() -- Annotate current line
marginalia.open_list() -- Open quickfix list
marginalia.open_manager() -- Open manager buffer
marginalia.search() -- Fuzzy search with fzf-lua- luacheck (linter)
- Install via luarocks:
luarocks install luacheck - Or system package manager:
sudo apt install lua-check(Ubuntu/Debian)
- Install via luarocks:
- stylua (formatter)
- Install via cargo:
cargo install stylua
- Install via cargo:
- pre-commit (hook manager)
- Install via pip:
pip install pre-commit
- Install via pip:
Install git hooks to automatically lint and format on commit:
make install-hooksRun tests with make:
make testLint code:
make lintFormat code:
make format