Inline Claude Code Transformations in Neovim - Without Plugins
TL;DR
Transform code selections in Neovim using Claude Code’s non-interactive mode and Vim’s built-in shell command feature - no plugins required. Select text, press <leader>ce
, type your transformation, done.
Quickly transform code selections in Neovim by piping them through Claude Code - convert between block styles, modernize syntax, add error handling, and more with just a visual selection and a prompt.
Requirements
- vim/nvim
- Claude Code
- That’s it!
The Idea
The inspiration for this came from watching this video from typecraft. In it, he mentions an “esoteric” command that can be used to pipe visual mode ranges into a shell command.
I was aware you could use :!
to execute shell commands but never made the jump of using it with a visual range until watching this video.
I’ve been experimenting a lot with Claude Code recently, and my mind jumped straight to this as a use case thanks to Anthropic’s unix-like philosophy towards this tool.
Claude Code is awesome in normal mode, but it can also be used in non-interactive mode making the possibilities for other workflows endless.
Implementation
Of course, you could just pipe lines directly into claude like:
:'<,'>!claude -p "correct the indentation here"
But this is equivalent to starting a chat with that prompt so you will get all the normal response fluff along with the corrected code making it useless as a one-liner.
The solution to this is to prepend the prompt with explicit instructions telling it exactly what you want, this can then be saved as a reusable mapping that can be quickly accessed from visual mode like so:
Example Transformations
Here are some practical ways to use this:
convert to multiline do/end block
- Transform Ruby single-line blocksmodernize hash syntax
- Update old Ruby hash rockets to modern syntaxadd error handling
- Wrap code in appropriate try/catch or begin/rescueextract to method
- Pull out selected code into a well-named functionconvert to ternary
- Simplify if/else statementsadd type annotations
- Add TypeScript or Ruby Sorbet types
This will ask for a prompt and then execute that prompt with the instructions on the range provided, returning only the transformed code. All without much overhead or any plugins, just using native vim features and the claude cli.
Tips
- Adjust the prompt prefix in
full_prompt
to match your coding style - Create preset transformations for common tasks
- Use
--output-format text
explicitly if you encounter formatting issues - Remember that Claude Code needs to be installed and authenticated first
📹 Watch a short demo of this in action
Why This Matters
This approach embodies the Unix philosophy - small, focused tools working together. No bloated plugins or no complex configurations, just Vim doing what it does best: text manipulation, working along with other tools.