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

> Source: <https://gist.github.com/Metehan-Altuntekin/32dabb02113007a4ee934d0a9c55ea14>
> Published: 2026-11-25 16:58:12+00:00

**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 the playbook "Working with AI coding agents": <https://wpnews.pro/playbooks/coding-agents>

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.

``` js
/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.
