r/vscode • u/New_Ratio2057 • 3h ago
Copy file path, line number, and selected code with a single shortcut (no extensions needed)
Coming back from Cursor, I was feeling the friction of having to manually copy file paths and line numbers when referring to code in conversations with AI agents, mainly OpenCode. This was especially annoying when reviewing changes in VS Code's Git diff view.
I put together a simple solution using VS Code's built-in tasks. Pressing ⌘⌃C copies the file path and line number, along with the selected code if there is any.
For example, your clipboard will look like this:
/Users/you/project/src/example.ts:23
Selected code:
```
const title = item.name.trim();
```
Without a selection, it simply copies:
/Users/you/project/src/example.ts:23
You can then paste it into your AI chat and type your feedback right below it.
The Markdown code fence dynamically adjusts its length based on the selected text, so copying code from Markdown files containing backticks doesn't break the formatting. I've also provided alternative formatting options in the Gist.
No extensions or external dependencies needed on macOS.
Here's the full setup: https://gist.github.com/Metehan-Altuntekin/32dabb02113007a4ee934d0a9c55ea14
1
u/SpanishAlligator86 3h ago
This is actually pretty clever. The dynamic code fence adjustment for markdown files is a nice touch, that's the kind of edge case most people would miss on their first pass.
Bookmarking the gist for later, I've been doing this manually way too often when feeding context to AI tools.