I Built a Claude Code Pop Menu Inside of Neovim

I Built a Claude Code Pop Menu Inside of Neovim

Kai Neovim AI Assistant

Kai: AI-powered coding in Neovim (click for full size)

I’ve been using AI to help with coding for a while now, but going back and forth between my code and AI was getting tedious—even with a highly optimized setup. So I integrated my Digital Assistant, Kai, directly into Neovim.

The Problem

When working with AI for code editing, you typically want one of these actions:

  • Replace selected code with an improved version
  • Insert new code based on context
  • Display information without modifying anything

Most AI integrations make you tell them exactly what to do. Kai somewhat figures it out from how you ask.

How It Works

This entire thing is based on a capability in Claude Code that is massively underdiscussed. I just think of it as command-line mode.

  • You can pipe into this thing
  • You could give it a string afterwards and it will just go and execute
  • You can even control how it uses different contexts and such.

Anyway, that’s what’s going on under the hood. It’s this command-line version of Claude Code that we’re actually calling with this plug-in.

Structure

The plugin has two main pieces:

  1. Lua plugin (kai-neovim.lua) – Handles Neovim integration, visual selections, and buffer management
  2. Shell script (kai-neovim.sh) – Processes context and communicates with the AI backend

Smart Context Handling

Kai Progress Window

Kai’s enhanced progress window showing detailed processing phases

The plugin always sends the entire buffer as context, but intelligently focuses on:

  • Selected text when you’re in visual mode
  • Cursor position when you’re in normal mode

This approach provides comprehensive context while enabling precise, targeted modifications based on your current selection or cursor position.

Intelligent Action Detection

The plugin lets you basically tell it anything, and it tries to work it out. Here are some examples.

  • “Replace with X” → Replaces selection
  • “Add a comment explaining” → Inserts after selection
  • “What does this do?” → Shows analysis in a popup
  • “Insert an appropriate image here.” → Creates a custom image and inserts it at that location

  • “Fix the error” → Replaces with corrected code

This is just completely insane. We can just send arbitrary things and have it kind of figure it out. Basically, command-line interaction with AI. And within your text editor.

The Code

This is the basic code for it, but keep in mind it’s a work in progress. It might be total garbage. And yes, I had Kai help me build it for sure.

Main Plugin Code

Save this as ~/.config/nvim/lua/kai-neovim.lua:

Shell Script

Create ~/.config/nvim/scripts/kai-neovim.sh:

Don’t forget to make the script executable:

Summary

All right, that should get you started with the structure for your own implementation.

And now you can talk directly to your AI from within Neovim!


Source link

About Cybernoz

Security researcher and threat analyst with expertise in malware analysis and incident response.