Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

🍿 Inject active filepath to integrated terminal in VS Code

Snacks (🍿) are my collection of recipes for solving problems. Often recorded (and cleaned up) from actual discussions where I'm involved in helping others with technical problems. Not all solutions are mine but I started collecting them in one place because you never know when you need one.

I use Notion as my headless CMS for this site and whenever I download a blog post from Notion to my repository, I inject its Notion ID to the front matter. This is handy because I can then re-download that post again using that ID later using my custom CLI tooling.

However, there's one step too many for the lazy days and I wanted to figure out a way to run the command for the currently active window so I don't have to manually copy anything.

Turns out, VS Code supports this!

I added the following to the keybindings JSON file

  "key": "ctrl+shift+t",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "'${file}'" }

and now when I hit CTRL + SHIFT + t, the file's full absolute path gets added to the terminal prompt and I can adjust my script to accept filenames and parse IDs from their frontmatter if they are a blog post fetched from Notion.

${file} is a variable and VS Code has a bunch of predefined variables you can use.