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:
- Open the Command Palette (CMD+SHIFT+P)
- Select "Preferences: Open Keyboard Shortcuts (JSON)"
- 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:
- The
whenclause ensures the shortcut only fires when the terminal is focused. ${relativeFile}is one of VS Code's predefined variable, see reference for full list.