Skip to content

nvim agent setup#25

Merged
Vilos92 merged 3 commits into
mainfrom
nvim-agent-context
Jun 22, 2026
Merged

nvim agent setup#25
Vilos92 merged 3 commits into
mainfrom
nvim-agent-context

Conversation

@Vilos92

@Vilos92 Vilos92 commented Jun 22, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

Release Notes

  • New Features
    • Added a :NvimCtx command in Neovim to output selected-text context (or full buffer when no selection exists)
    • Added an nvim-ctx utility to fetch Neovim context from the active Neovim session/server
  • Documentation
    • Updated AI Neovim guidance to proactively extract Neovim context (including line ranges) and request pasted content only if context extraction isn’t available
  • Internal Updates
    • Ensured Neovim context support is loaded as part of the Neovim startup configuration

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 37fc1cfc-6b20-4799-abd1-529558346297

📥 Commits

Reviewing files that changed from the base of the PR and between f5bb929 and fa9bcc1.

📒 Files selected for processing (1)
  • zsh/.local/bin/nvim-ctx
🚧 Files skipped from review as they are similar to previous changes (1)
  • zsh/.local/bin/nvim-ctx

📝 Walkthrough

Walkthrough

Adds a nvim-ctx pipeline: a new Neovim Lua module (greg/ctx.lua) that serializes the current visual selection or full buffer to JSON and exposes it as _G.NvimCtxJSON, a shell script (nvim-ctx) that locates the running Neovim server socket via tmux and calls that function remotely, and a CLAUDE.md instruction to use the tool proactively.

Changes

Neovim Context Bridge

Layer / File(s) Summary
Neovim Lua context module and registration
nvim/.config/nvim/lua/greg/ctx.lua, nvim/.config/nvim/lua/greg/init.lua
ctx.lua implements M.json() to read visual selection marks and file path (falling back to the full buffer), returns a JSON string with file, start_line, end_line, and text, exposes it as _G.NvimCtxJSON, and registers a :NvimCtx user command. init.lua adds require("greg.ctx") to load the module at startup.
nvim-ctx shell script bridge
zsh/.local/bin/nvim-ctx
New script accepts an optional tmux session name, finds the pane whose command is nvim, resolves the actual nvim PID (accounting for server-child forking), searches /var/folders for the matching server socket, then invokes nvim --server <socket> --remote-expr 'v:lua.NvimCtxJSON()'. Exits with status 1 and a stderr message if any step fails.
Claude assistant instructions
claude/.claude/CLAUDE.md
Adds a guidance block telling the assistant to proactively call nvim-ctx when Greg references Neovim code, parse the JSON output, and fall back to asking Greg to paste content if the command fails.

Sequence Diagram(s)

sequenceDiagram
  participant Claude as Claude Assistant
  participant nvim_ctx as nvim-ctx script
  participant tmux as tmux
  participant socket as Neovim Server Socket
  participant NvimCtxJSON as v:lua.NvimCtxJSON()

  Claude->>nvim_ctx: run nvim-ctx [session]
  nvim_ctx->>tmux: list-panes, find nvim pane
  tmux-->>nvim_ctx: pane PID
  nvim_ctx->>nvim_ctx: resolve nvim PID, search /var/folders for socket
  nvim_ctx->>socket: nvim --server <socket> --remote-expr
  socket->>NvimCtxJSON: evaluate v:lua.NvimCtxJSON()
  NvimCtxJSON-->>socket: JSON {file, start_line, end_line, text}
  socket-->>nvim_ctx: JSON string
  nvim_ctx-->>Claude: JSON context output
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Hippity-hop through the tmux maze,
A socket found through /var/folders haze,
The selection's text, now JSON-bright,
Sent to Claude on a whisker's flight.
No more pasting — just nvim-ctx and done! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'nvim agent setup' is vague and generic, using non-descriptive terms that don't convey meaningful information about the specific changeset. Consider a more specific title that describes the primary change, such as 'Add Neovim context extraction tool for AI agent' or 'Implement nvim-ctx tool for code context capture'.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nvim-agent-context

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@nvim/.config/nvim/lua/greg/ctx.lua`:
- Around line 5-16: The code uses stale visual marks (line("'<") and line("'>"))
to detect if a selection is active, but these marks persist from previous
selections even when no selection is currently active. Instead of relying on
mark existence, use Vim's active mode detection to determine if a visual
selection is currently active. Check the current mode using Vim's mode detection
function to distinguish between an active visual selection and a previous
selection that no longer exists, then conditionally fetch either the selected
lines or the entire buffer accordingly.

In `@zsh/.local/bin/nvim-ctx`:
- Around line 17-24: The issue is that line 17 uses pgrep to get the first child
process without verifying it's nvim, which can select the wrong process and
cause line 22 to incorrectly report nvim is not running. Instead of taking the
first child with head -1, modify the pgrep command to filter for child processes
that match the nvim command specifically, or iterate through the child processes
to find the one where the command is nvim, ensuring the correct process is
identified before the verification check on line 22.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eca9daa3-b61e-48a7-bcd7-40b52159f856

📥 Commits

Reviewing files that changed from the base of the PR and between db1b2f5 and 95f4124.

📒 Files selected for processing (4)
  • claude/.claude/CLAUDE.md
  • nvim/.config/nvim/lua/greg/ctx.lua
  • nvim/.config/nvim/lua/greg/init.lua
  • zsh/.local/bin/nvim-ctx

Comment on lines +5 to +16
local sl = vim.fn.line("'<")
local el = vim.fn.line("'>")
local file = vim.fn.expand("%:p")
local text

if sl == 0 then
sl = 1
el = vim.fn.line("$")
text = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n")
else
text = table.concat(vim.fn.getline(sl, el) --[[@as string[] ]], "\n")
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Use active mode detection instead of stale visual marks.

Line 10 currently treats any existing '< mark as an active selection. That can return a previous selection during normal-mode remote calls, violating the “selection if active, otherwise full buffer” contract.

Proposed fix
 function M.json()
-  local sl = vim.fn.line("'<")
-  local el = vim.fn.line("'>")
+  local sl = vim.fn.line("'<")
+  local el = vim.fn.line("'>")
   local file = vim.fn.expand("%:p")
   local text
 
-  if sl == 0 then
+  local mode = vim.api.nvim_get_mode().mode
+  local has_active_visual = (mode == "v" or mode == "V" or mode == "\22")
+
+  if not has_active_visual then
     sl = 1
     el = vim.fn.line("$")
     text = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n")
   else
     text = table.concat(vim.fn.getline(sl, el) --[[`@as` string[] ]], "\n")
   end
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@nvim/.config/nvim/lua/greg/ctx.lua` around lines 5 - 16, The code uses stale
visual marks (line("'<") and line("'>")) to detect if a selection is active, but
these marks persist from previous selections even when no selection is currently
active. Instead of relying on mark existence, use Vim's active mode detection to
determine if a visual selection is currently active. Check the current mode
using Vim's mode detection function to distinguish between an active visual
selection and a previous selection that no longer exists, then conditionally
fetch either the selected lines or the entire buffer accordingly.

Comment thread zsh/.local/bin/nvim-ctx Outdated
@Vilos92 Vilos92 changed the title agent setup nvim agent setup Jun 22, 2026
@Vilos92 Vilos92 merged commit ae2d505 into main Jun 22, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant