{"slug": "copy-file-path-line-number-and-selected-text-in-vs-code-via-a-cmd-ctrl-c", "title": "Copy file path, line number, and selected text in VS Code via a CMD+CTRL+C", "summary": "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.", "body_md": "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`.\n\n- With no selection, it copies just the absolute `path:line` .\n- With a selection, it copies `path:line` followed by the selected text in a Markdown code block.\n- The code block delimiter dynamically adjusts its length to avoid conflicts with backticks in the selected text. Alternative formatting options are also provided.\n\nPaste 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.\n\nNo extensions or external dependencies are required.\n\n``` js\n/Users/you/Code/my-project/app/src/example.ts:23\n\nSelected code:\n```\nconst title = item.name.trim();\nFor a multiline selection, the reference uses the active cursor line, and the code block includes the full selection.\n\n```\n/Users/you/Code/my-project/app/src/example.ts:23\n```\n\nThe 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.\n\nFor example, if the selected text contains three consecutive backticks, the outer code block will use four.\n\nOpen 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.\n\n```\n{\n  \"version\": \"2.0.0\",\n  \"tasks\": [\n    {\n      \"label\": \"Copy path and line\",\n      \"type\": \"process\",\n      \"command\": \"/bin/bash\",\n      \"args\": [\"-c\", \"printf '%s' \\\"$1\\\" | /usr/bin/pbcopy\", \"--\", \"${file}:${lineNumber}\"],\n      \"presentation\": {\n        \"reveal\": \"never\",\n        \"close\": true,\n        \"echo\": false,\n        \"focus\": false,\n        \"showReuseMessage\": false,\n      },\n      \"problemMatcher\": [],\n    },\n    {\n      \"label\": \"Copy path and selection\",\n      \"type\": \"process\",\n      \"command\": \"/bin/bash\",\n      \"args\": [\n        \"-c\",\n        \"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\",\n        // alternative 1: \"printf '%s\\\\n\\\\n<selection>\\\\n%s\\\\n</selection>\\\\n\\\\n' \\\"$1\\\" \\\"$2\\\" | /usr/bin/pbcopy\",\n        // alternative 2: \"printf '%s\\\\n\\\\n```\\\\n%s\\\\n```\\\\n\\\\n' \\\"$1\\\" \\\"$2\\\" | /usr/bin/pbcopy\",\n        // alternative 3: \"printf '%s\\\\n\\\\n--- BEGIN SELECTION ---\\\\n%s\\\\n--- END SELECTION ---\\\\n\\\\n' \\\"$1\\\" \\\"$2\\\" | /usr/bin/pbcopy\",\n        \"--\",\n        \"${file}:${lineNumber}\",\n        \"${selectedText}\",\n      ],\n      \"presentation\": {\n        \"reveal\": \"never\",\n        \"close\": true,\n        \"echo\": false,\n        \"focus\": false,\n        \"showReuseMessage\": false,\n      },\n      \"problemMatcher\": [],\n    },\n  ]\n}\n```\n\nRun **Preferences: Open Keyboard Shortcuts (JSON)** from the Command Palette. Add these two entries to your existing array.\n\n```\n[\n  {\n    \"key\": \"cmd+ctrl+c\",\n    \"command\": \"workbench.action.tasks.runTask\",\n    \"args\": \"Copy path and selection\",\n    \"when\": \"editorTextFocus && resourceScheme == file && editorHasSelection\",\n  },\n  {\n    \"key\": \"cmd+ctrl+c\",\n    \"command\": \"workbench.action.tasks.runTask\",\n    \"args\": \"Copy path and line\",\n    \"when\": \"editorTextFocus && resourceScheme == file && !editorHasSelection\",\n  },\n]\n```\n\nOpen the Command Palette with **⌘⇧P**, run **Tasks: Run Task**, and select either **Copy path and line** or **Copy path and selection**.\n\nIf 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.", "url": "https://wpnews.pro/news/copy-file-path-line-number-and-selected-text-in-vs-code-via-a-cmd-ctrl-c", "canonical_source": "https://gist.github.com/Metehan-Altuntekin/32dabb02113007a4ee934d0a9c55ea14", "published_at": "2026-11-25 16:58:12+00:00", "updated_at": "2026-11-25 17:45:18.607302+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["VS Code", "macOS", "pbcopy"], "also_reported_by": [], "takeaway": "Binding ⌘⌃C to a VS Code task that pipes ${file}:${lineNumber} and ${selectedText} through pbcopy produces a paste-ready path:line reference with a code fence that grows past any backticks in the selection.", "recommendation": {"advice": "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.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/copy-file-path-line-number-and-selected-text-in-vs-code-via-a-cmd-ctrl-c", "markdown": "https://wpnews.pro/news/copy-file-path-line-number-and-selected-text-in-vs-code-via-a-cmd-ctrl-c.md", "text": "https://wpnews.pro/news/copy-file-path-line-number-and-selected-text-in-vs-code-via-a-cmd-ctrl-c.txt", "jsonld": "https://wpnews.pro/news/copy-file-path-line-number-and-selected-text-in-vs-code-via-a-cmd-ctrl-c.jsonld"}}