VS Code: Open terminal in directory of currently opened file

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
5 min

banner

# Table of Contents

  1. VS Code: Open terminal in directory of currently opened file
  2. Changing the current working directory of the Terminal in VS Code

# VS Code: Open terminal in directory of currently opened file

When you open the terminal in VS Code, it gets opened in the root directory of the project (workspace).

For example, if you press Ctrl + ` (backtick) or Cmd + ` on macOS, the terminal automatically opens in your project's root directory.

However, in some cases, it is more convenient to have the terminal open in the directory of the current file.

To open the terminal in the directory of the currently opened file:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Keyboard Shortcuts and select Preferences: Open Keyboard Shortcuts.

preferences open keyboard shortcuts

  1. Click on the Open Keyboard Shortcuts (JSON) icon to the left.

open keyboard shortcuts

  1. Add the following object to your keybindings.json file.
keybindings.json
[ { "key": "ctrl+shift+c", "command": "workbench.action.terminal.newWithCwd", "args": { "cwd": "${fileDirname}" } } ]

If you use macOS, use cmd instead of ctrl.

keybindings.json
{ "key": "cmd+shift+c", "command": "workbench.action.terminal.newWithCwd", "args": { "cwd": "${fileDirname}" } }

The ${fileDirname} part is a variable that gets resolved to the path of the currently opened file's folder.

To test the behavior:

  1. Open a file.
  2. Press Ctrl + Shift + C or Cmd + Shift + C on macOS.
  3. The terminal should automatically open in the directory of the currently opened file.

Here is a short clip that demonstrates how this works.

open terminal in directory of currently opened file

You can customize the Ctrl + Shift + C keyboard shortcut according to your preference.

This approach enables you to keep the default behavior of opening the terminal in the project's root directory when you issue the Toggle terminal command or press Ctrl + `.

For example, if you:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type toggle terminal and select View: Toggle Terminal.

vscode open terminal

Your terminal will open in your project's root directory.

default terminal opens in root directory

# Changing the current working directory of the Terminal in VS Code

If you need to change the current working directory of the terminal:

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type terminal cwd and set your preferred path in the Terminal > Integrated: Cwd field.

change terminal current working directory vscode

You might specify a relative or an absolute path.

For example, I've set the current working directory of the terminal to my-folder.

So when I open my terminal it opens in the my-folder directory.

open terminal in specified directory

You can also use an absolute path:

  • on Windows: C:\\path\\to\\your\\directory.
  • on macOS or Linux: /path/to/your/directory.

If the current working directory hasn't changed successfully in your case, click on the trash bin icon at the top right corner of the terminal to close it.

click trash bin icon

Clicking on the X icon keeps the terminal running in the background.

You can also set the current working directory using a variable.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type terminal cwd and set the Terminal > Integrated: Cwd field to ${fileDirname}.

set cwd using variable

The ${fileDirname} part is a variable that gets resolved to the path of the currently opened file's folder.

To test this behavior:

  1. Close your currently opened terminals by clicking on the trash bin icon.
  2. Open a file that is located in a nested directory.
  3. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type toggle terminal and select View: Toggle Terminal.

vscode open terminal

The terminal will open in the directory that contains the currently opened file.

You can view all of the available predefined variables in this section of the docs.

Here are some examples:

  • ${userHome} - resolves to the user's home directory, e.g. /home/your-username.
  • ${workspaceFolder} - resolves to your project's root directory, e.g. /home/your-username/your-project.
  • ${pathSeparator} resolves to the path separator character of the current operating system - / on macOS and Linux, \ on Windows.

# Changing the current working directory of the terminal in settings.json

You can also use your settings.json file to change the current working directory of your terminal.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Add the following line to your settings.json file.
settings.json
"terminal.integrated.cwd": "${fileDirname}"

change cwd in settings json

As previously noted, you can set the property to a relative path:

  • on Windows: myFolder\nestedFolder
  • on macOS and Linux: myFolder/nestedFolder

Or an absolute path:

  • on Windows: C:\\path\\to\\your\\directory
  • on macOS or Linux: /path/to/your/directory

# Only changing the current working directory of the terminal for the current project

In some cases, you might only want to change the current working directory of the terminal for the current project and not globally.

This is done using a local .vscode/settings.json file.

The setting is only applied to your current project and overrides any configuration in your global settings.json file.

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Add the following code to your settings.json file.

.vscode/settings.json
{ "terminal.integrated.cwd": "${fileDirname}" }

only update current working terminal directory for current project

The properties in .vscode/settings.json only apply to the current project (workspace) and override any configuration present in your global settings.json file.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.