How to change the terminal to the current directory in visual studio code ? (hotkey) [duplicate]
Asked Answered
K

3

46

In class we had a hotkey for using the terminal with the currently choosen directory. I fixed an issue now with the debugger and everything seems to run smoothly now. Yet, what hotkey fixes this issue?

Kahlil answered 13/4, 2019 at 17:31 Comment(2)
code.visualstudio.com/docs/editor/integrated-terminalUl
If I use the View: Toggle Integrated Terminal command the terminal opens just as toggle terminal. But, the directory is not opening.Kahlil
P
50

With VSCode 1.39 (Sept. 2019), no more plugin needed.
You now can "Open new terminals with custom working directories"

There is a new command that allows the creation of terminals with a custom current working directory (cwd):

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

You can create your own keyboard shortcuts to open new terminals in any number of handy working directories.
The cwd value can either be a normal path or a variable.


ChrisoLosoph mentions in the comments:

What to do with the pasted code snippet:

  • press F1,
  • Enter, and
  • select "Open Keyboard Shortcuts (JSON)"
  • and paste it there.
Polity answered 9/10, 2019 at 20:18 Comment(11)
Haven't needed a plugin to change the current terminal working directory for a few months.Stadiometer
@Stadiometer Agreed. This is for creating a new terminal.Polity
Yes, as detailed in my linked answer ;>}Stadiometer
Thanks, but for a newbie coming from Atom, what do I actually have to do?! Where is that snippet?Preoccupy
@Preoccupy You can see it in action in github.com/microsoft/vscode/issues/156461. But that issue also illustrates it might not always work as well as expected.Polity
The shortcut mentioned above is not working for me, - CTRL+Shift+H is already a keyboard shortcut for workbench.action.replaceInFiles. (Probably a change in newer VS Code versions)Robbegrillet
@Robbegrillet Thank you for the feedback. I believe this is now Ctrl+Shift+c.Polity
Ctrl+Shift+c opens Windows Command prompt (cmd), it doesn't open the integrated terminal. Is that the expected behaviour?Robbegrillet
@Robbegrillet Yes it is. I did not found newWithCwd with the latest VSCode versions.Polity
If others didn't understand either, what to do with the pasted code snippet: press F1, enter and select "Open Keyboard Shortcuts (JSON)" and paste it there.Swahili
@Swahili Good point, thank you for your feedback. I have included your comment in the answer for more visibility.Polity
S
27

For a hotkey to quickly set your terminal folder to your current directory, see How to quickly change shell folder to match the currently open file

{
  "key": "alt+t",
  "command": "workbench.action.terminal.sendSequence",
  "args": {"text": "cd '${fileDirname}'\u000D"}
},

This will change your current terminal, not open a new terminal - if you want to do that see the link above as well for the new command recently added to vscode.

Note that on windows, you must use the following instead:

"args": {"text": "cd /d \"${fileDirname}\"\u000D"}

This is because on Windows, the /d parameter must be used with cd to switch drives.

Stadiometer answered 10/10, 2019 at 2:0 Comment(1)
This will also change focus (copy paste to understand) { "key": "ctrl+shift+'", "command": "runCommands", "args": { "commands": [ "workbench.action.terminal.focus", { "command": "workbench.action.terminal.sendSequence", "args": { "text": "cd '${fileDirname}'\u000D" } } ] } },Stutter
V
11

By default, the terminal will open at the folder that is opened in the Explorer. The hotkey for that depends on your operating system, in my case, on macOS, it's + `, on Windows I think it's ctrl + j.

If you want to open at a specific folder you can change that behaviour with this setting:

{
    "terminal.integrated.cwd": "/home/user"
}

If you want to open at the current file's directory you can install an extension like Terminal Here.

You can change the shortcuts/hotkeys for many actions on Code. For that open the Keyboard Shortcuts editor under File > Preferences > Keyboard Shortcuts and search for Toggle Integrated Terminal.

enter image description here

Vaucluse answered 13/4, 2019 at 17:56 Comment(5)
Thank you for your answer! Our professor showed us a hotkey, so I want to ask now in the industry would people use a hotkey or terminal here. Is there not a hotkey I can set ? I found two more convenient ways for me. Where do I add this in the json.file for user settings ?Kahlil
Hi @DimitriWilliams, I edited my answer, check it out. Let me know if you need more help.Vaucluse
Hi @Diogo Rocha this is exactly what is not working. I can open the terminal, but not the directory where I opened the file, where I am writing code. I changed File > Preferences > Keyboard Shortcuts and search for Toggle Integrated Terminal. To a Key Command / Hotkey and it only opens the workspace folder, not the folder with the code inside the terminal.Kahlil
So I would suggest you to install the "Terminal Here" extension, and set a custom keyboard shortcut for that.Vaucluse
That helped and worked immediately, I will remember this till the end of the extension. Thank you!Kahlil

© 2022 - 2024 — McMap. All rights reserved.