cd /news/developer-tools/copy-file-path-line-number-and-selec… · home › topics › developer-tools › article
[ARTICLE · art-176266] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

Copy file path, line number, and selected text in VS Code via a CMD+CTRL+C

A developer published a macOS-only VS Code setup that binds ⌘⌃C to copy an absolute path:line reference, plus the selected text in a dynamically sized Markdown code block, for pasting into code reviews or coding-agent chats. The setup uses VS Code's built-in tasks and macOS's pbcopy, with no extensions or external dependencies, and adjusts the code fence length to avoid conflicts with backticks in the selection. Two task definitions and two keybindings are added to the user's existing tasks and keybindings JSON files.

recommendation

Bind a shortcut that copies path:line plus the selected code as a fenced block so coding agents get exact file references; grow the fence length to avoid backtick conflicts.

more in playbook: Working with AI coding agents
by read2 min views3 publishedNov 25, 2026

Use ⌘⌃C on macOS to copy a reference for a code review or a coding agent. The shortcut uses VS Code’s built-in tasks and macOS’s pbcopy.

  • With no selection, it copies just the absolute path:line .
  • With a selection, it copies path:line followed by the selected text in a Markdown code block.
  • The code block delimiter dynamically adjusts its length to avoid conflicts with backticks in the selected text. Alternative formatting options are also provided.

Paste into your chat and type your feedback after the reference. In a Git diff, use the editable side so the line number refers to the working file.

No extensions or external dependencies are required.

/Users/you/Code/my-project/app/src/example.ts:23

Selected code:

const title = item.name.trim(); For a multiline selection, the reference uses the active cursor line, and the code block includes the full selection.

/Users/you/Code/my-project/app/src/example.ts:23

The script starts with three backticks and adds more until the delimiter no longer appears anywhere in the selected text. This prevents nested code blocks from prematurely closing the outer code block.

For example, if the selected text contains three consecutive backticks, the outer code block will use four.

Open the Command Palette with ⌘⇧P and run Tasks: Open User Tasks. If you already have tasks, add these two objects to your existing tasks array.

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Copy path and line",
      "type": "process",
      "command": "/bin/bash",
      "args": ["-c", "printf '%s' \"$1\" | /usr/bin/pbcopy", "--", "${file}:${lineNumber}"],
      "presentation": {
        "reveal": "never",
        "close": true,
        "echo": false,
        "focus": false,
        "showReuseMessage": false,
      },
      "problemMatcher": [],
    },
    {
      "label": "Copy path and selection",
      "type": "process",
      "command": "/bin/bash",
      "args": [
        "-c",
        "fence='```'; while [[ \"$2\" == *\"$fence\"* ]]; do fence+='`'; done; printf '%s\\n\\nSelected code:\\n%s\\n%s\\n%s\\n\\n' \"$1\" \"$fence\" \"$2\" \"$fence\" | /usr/bin/pbcopy",
        // alternative 1: "printf '%s\\n\\n<selection>\\n%s\\n</selection>\\n\\n' \"$1\" \"$2\" | /usr/bin/pbcopy",
        // alternative 2: "printf '%s\\n\\n```\\n%s\\n```\\n\\n' \"$1\" \"$2\" | /usr/bin/pbcopy",
        // alternative 3: "printf '%s\\n\\n--- BEGIN SELECTION ---\\n%s\\n--- END SELECTION ---\\n\\n' \"$1\" \"$2\" | /usr/bin/pbcopy",
        "--",
        "${file}:${lineNumber}",
        "${selectedText}",
      ],
      "presentation": {
        "reveal": "never",
        "close": true,
        "echo": false,
        "focus": false,
        "showReuseMessage": false,
      },
      "problemMatcher": [],
    },
  ]
}

Run Preferences: Open Keyboard Shortcuts (JSON) from the Command Palette. Add these two entries to your existing array.

[
  {
    "key": "cmd+ctrl+c",
    "command": "workbench.action.tasks.runTask",
    "args": "Copy path and selection",
    "when": "editorTextFocus && resourceScheme == file && editorHasSelection",
  },
  {
    "key": "cmd+ctrl+c",
    "command": "workbench.action.tasks.runTask",
    "args": "Copy path and line",
    "when": "editorTextFocus && resourceScheme == file && !editorHasSelection",
  },
]

Open the Command Palette with ⌘⇧P, run Tasks: Run Task, and select either Copy path and line or Copy path and selection.

If VS Code opens a task picker instead of running the task directly, check that both tasks are saved in the same tasks array and that the labels match the keybindings exactly.

── more in #developer-tools 4 stories · sorted by recency
── more on @vs code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/copy-file-path-line-…] indexed:0 read:2min 2026-11-25 · —