Last updated: Apr 6, 2024
Reading time·5 min
When you open the terminal in VS Code, it gets opened in the root directory of the project (workspace).
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:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.keybindings.json
file.[ { "key": "ctrl+shift+c", "command": "workbench.action.terminal.newWithCwd", "args": { "cwd": "${fileDirname}" } } ]
If you use macOS, use cmd
instead of ctrl
.
{ "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:
Ctrl
+ Shift
+ C
or Cmd
+ Shift
+ C
on macOS.Here is a short clip that demonstrates how this works.
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:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Your terminal will open in your project's root directory.
If you need to change the current working directory of the terminal:
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
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.
You can also use an absolute path:
C:\\path\\to\\your\\directory
./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.
Clicking on the X
icon keeps the terminal running in the background.
You can also set the current working directory using a variable.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
${fileDirname}
.The ${fileDirname}
part is a variable that gets resolved to the path of the
currently opened file's folder.
To test this behavior:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.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.You can also use your settings.json
file to change the current working
directory of your terminal.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
"terminal.integrated.cwd": "${fileDirname}"
As previously noted, you can set the property to a relative path:
myFolder\nestedFolder
myFolder/nestedFolder
Or an absolute path:
C:\\path\\to\\your\\directory
/path/to/your/directory
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.
In the root directory of your project, create a .vscode folder.
Create a settings.json
file in the .vscode
folder.
Add the following code to your settings.json
file.
{ "terminal.integrated.cwd": "${fileDirname}" }
The properties in .vscode/settings.json
only apply to the current project
(workspace) and override any configuration present in your global
settings.json
file.
You can learn more about the related topics by checking out the following tutorials: