Cursor's Cmd+L in Neovim

A single Lua keymap that copies @file:line-range to your system clipboard, so you can attach any selection to Claude Code the same way Cursor lets you with Cmd+L.

One thing I missed when I moved from Cursor to Neovim + Claude Code was the context attachment shortcut. In Cursor, Cmd+K opens an inline prompt where the current file is already attached. Cmd+L does the same for the chat panel. You select some code, hit the shortcut, and you’re talking about exactly that snippet.

Claude Code has no equivalent built-in for Neovim. There’s no IDE extension, no sidebar — just a CLI running in a terminal. You’re on your own to bridge the gap.

What I tried first

The most common advice online is: run Claude Code in a tmux pane next to Neovim, yank text to system clipboard with "+y, switch panes, paste. Simple, zero setup.

It works, but the friction adds up. You lose the line range context. Claude sees floating code with no file name and no location. If you’re asking about a function, you almost always need to say where it lives, and typing that by hand every time gets old fast.

The key insight

Claude Code understands @file:line-range syntax. If you paste @/absolute/path/to/file.lua:80-92 into the prompt, Claude fetches those exact lines and treats them as context — the same way it does when you use @ to attach a file from the CLI.

So the problem reduces to: make Neovim write that string to the clipboard automatically when I select code.

The keymap

Two keymaps share the same mnemonic (<leader>yp — yank path). Normal mode copies just the file path. Visual mode copies the full @file:line-range reference (Yes, I have a command to copy the absolute path, it is super useful!).

-- Normal mode: copy absolute file path
vim.keymap.set('n', '<leader>yp', function()
local path = vim.fn.expand '%:p'
vim.fn.setreg('+', path)
vim.notify('Copied: ' .. path)
end, { desc = '[Y]ank file [p]ath' })
-- Visual mode: copy @file:line or @file:start-end
vim.keymap.set('v', '<leader>yp', function()
local path = vim.fn.expand '%:p'
local start_line = vim.fn.line 'v'
local end_line = vim.fn.line '.'
if start_line > end_line then
start_line, end_line = end_line, start_line
end
local ref = start_line == end_line
and string.format('@%s:%d', path, start_line)
or string.format('@%s:%d-%d', path, start_line, end_line)
vim.fn.setreg('+', ref)
vim.notify('Copied: ' .. ref)
end, { desc = '[Y]ank file [p]ath with line range' })

A few things worth noting:

How to use it

  1. Open any file in Neovim.
  2. Enter visual mode (v, V, or Cmd+V) and select the lines you want to reference.
  3. Press <leader>yp. A notification appears showing the copied reference.
  4. Switch to your Claude Code terminal and paste (Cmd+V or middle-click).

The pasted text looks like this:

@/Users/jcconnects/.dotfiles/nvim/init.lua:17-29

Claude Code reads the file at that path, extracts lines 17–29, and includes them as context for your prompt.

Proof it works

During the session where I built this, I tested it immediately by copying a reference and pasting it raw into Claude Code with no other prompt:

@/Users/jcconnects/.dotfiles/nvim/init.lua:17-29

Claude’s response:

Yes, I can see it. That’s lines 17–29 of your init.lua:

vim.o.signcolumn = 'yes'
vim.o.updatetime = 250
vim.o.timeoutlen = 300
vim.o.splitright = true
vim.o.splitbelow = true
vim.o.tabstop = 2
vim.o.shiftwidth = 2
vim.o.expandtab = true
vim.o.softtabstop = 2
vim.o.list = true
vim.opt.listchars = { tab = '» ', trail = '·', nbsp = '␣' }
vim.o.inccommand = 'split'
vim.o.cursorline = true

The keymap is working. That’s your editor options block.

It fetched exactly those lines. No copy-paste of the code content required.

What this replaces

The workflow now feels close to what Cursor offers:

CursorNeovim + Claude Code
Select code, Cmd+K → inline prompt with context attached<leader>yp → switch to Claude Code → paste reference + type prompt
Cmd+L → chat with file attached<leader>yp → switch to Claude Code → paste reference

Not identical — you still switch windows — but the context is precise and zero-friction once the keymap is in muscle memory.

<leader>yp covers 90% of the workflow. Twenty-something lines of Lua, no plugins, no external dependencies.

Conclusion

Cursor’s Cmd+L is convenient because it removes a mental step: you don’t have to tell the AI where the code is, it already knows. That’s the whole trick — and it turns out you can replicate it in Neovim with a single keymap and no plugins.

The @file:line-range syntax is the bridge. Once you know it exists, the rest is just Lua glue to put the right string on your clipboard. If you live in Neovim and use Claude Code, this is worth the five minutes it takes to add.


João Cordeiro is a DevOps and SRE specialist who loves building connections between people and technology.

← All articles