TIL: VS Code keybinding to insert the active file path into the terminal

May 12, 2026 • TIL, VS Code

I frequently need to insert the relative path of the active file into the terminal. This is super useful, for example when running tests (e.g. pytest path/to/mytests.py), or for referencing a file when working in a terminal based coding agent such as Claude Code.

For this, I use a custom keyboard shortcut in VS Code, mapped to CMD+K plusR.

Here is how to set it up:

  1. Open the Command Palette (CMD+SHIFT+P)
  2. Select "Preferences: Open Keyboard Shortcuts (JSON)"
  3. Add the following object to the JSON array in keybindings.json:
  {
    "key": "cmd+k r",
    "command": "workbench.action.terminal.sendSequence",
    "args": {
      "text": "${relativeFile}"
    },
    "when": "terminalFocus"
  }

Notes:


Recent posts