← All articles

· 1 min read

Automatically Loading the .env File in Your VS Code Embed Terminal

Laptop with source code open in an editor on a white desk, next to a potted plant

Photo by Clément Hélardot on Unsplash

Simple, this example is based on a Mac with Apple chip.
But you can easily adapt for your environment.

How to Do It

1 — Open the file ~/.zprofile (create it if it doesn’t exist);

2 — Add the following code:

# Loading `.env` on VS Code embeded terminal
if [[ "$TERM_PROGRAM" == "vscode" && -f ".env" ]]; then
  source .env && \
  echo "✅ loaded .env"
fi

3 — Save the file;

4 — Close and reopen your embedded terminal in VS Code;

5 — Voilà! Your environment variables are now available in your terminal session;

By following these steps, you can automate the process of loading your .env file in the VS Code terminal, making it more convenient to work with your environment variables.

← All articles