Inline Claude Code Transformations in Neovim - Without Plugins

• 3 min read

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:

-- Claude transformation command vim.api.nvim_create_user_command('ClaudeTransform', function(opts) local prompt = vim.fn.input('Prompt: ') if prompt ~= '' then -- Prepend instructions for clean output local full_prompt = 'You are a code transformer, working on single blocks of text in a code editor. ' .. 'Output ONLY the transformed code. No markdown, no explanation. ' .. 'No backticks or formatting whatsoever. Task: ' .. prompt -- Execute the transformation on the range vim.cmd(opts.line1 .. ',' .. opts.line2 .. '!claude -p ' .. vim.fn.shellescape(full_prompt)) else vim.notify('Transformation cancelled', vim.log.levels.WARN) end end, { range = true }) -- Visual mode mapping for Claude transformation vim.api.nvim_set_keymap('v', '<leader>ce', ':ClaudeTransform<CR>', { noremap = true, silent = true, desc = "Claude transform selection" })

Example Transformations

Here are some practical ways to use this:

  • convert to multiline do/end block - Transform Ruby single-line blocks
  • modernize hash syntax - Update old Ruby hash rockets to modern syntax
  • add error handling - Wrap code in appropriate try/catch or begin/rescue
  • extract to method - Pull out selected code into a well-named function
  • convert to ternary - Simplify if/else statements
  • add 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.