17

With single file opened in vscode, when I open integrated Powershell it always starts in my $HOME folder.

Is there any way to quickly switch to the directory of the currently shown file without having to manually cd into it?

Something like cd $vsCurrentFileDirectory.

Currently, I right click the tab and copy path then cd (Split-Path <CTRL-v>).

Nick Schmidt
  • 1,427
  • 1
  • 13
  • 22
majkinetor
  • 8,730
  • 9
  • 54
  • 72

5 Answers5

28

EDIT: A new command will be added in v1.39 to make this more straightforward, see release notes. The example keybinding given is:

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

which does indeed work in the Insider's Build. This will create a new terminal though, not modify an existing terminal.


[Original Answer]: This will change the current terminal.

You can set up a keybinding to do this pretty easily:

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

The \u000D is a return so the command triggers immediately.

Also note that I put the '${fileDirname}' in quotes in case your directory name has spaces in it.

The keybinding will work whether focus is in the terminal or the file.


Suggested edit to be tested :

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.


Also see shortcut to change directory in Powershell and cmd for additional info in case you are changing drive letters and escaping double quotes in Powershell.

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

with discussion of the /d flag. Thanks to @skataben for the additional info.


Alternatively, there is an extension to do this: terminal-here, but the keybinding actually works faster. The sendSequence and variable substitution functionality was not available when that extension was created.

Finally, if you right-click on a folder in the explorer, there is an Open in Terminal option there (and corresponding command). Which means you could use that command in a keybinding like so:

{
  "key": "alt+t",
  "command": "openInTerminal"
}

But my first sendSequence keybinding remains the fastest way to do this.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • 3
    Only keybinding is correct here as other options all create new terminal, not change folder in existing one. – majkinetor Aug 06 '19 at 12:04
  • 2
    In keybindings.json. Open the Keyboard Shortcuts from the Gear icon in lower left. Then click on the icon in the upper left (just to the left of the split editor icon) that opens keybindings.json. – Mark Nov 08 '19 at 21:18
  • 1
    I get `cd '${fileDirname}' bash: cd: ${fileDirname}: No such file or directory` ? – Nk.Pl Nov 21 '19 at 17:17
  • Hmm, I use it everyday on bash, no problems. You are using the `Original Answer` code above? It has to be used like that - through a `sendSequence` command so that the variable `${fileDirname}` can be resolved. – Mark Nov 21 '19 at 17:30
  • `sendSequence` works on my local project, but failed on vscode remote project, the error is `'${fileDirname}' can not be resolved. Please open an editor.` but the `newWithCwd ` command works both local and remove environment. It's might be a bug? – Li Jinyao Sep 16 '20 at 02:10
  • where in the world i will put above json? – sairfan Apr 29 '21 at 21:21
  • 2
    @sairfan They are all keybindings, so keybindings.json. `Command Palette/Preferences: Open Keyboard Shortcuts (JSON)` – Mark Apr 29 '21 at 23:18
  • Thanks I'm new to VS Code did not understand what's going on there, thanks for help. – sairfan Apr 30 '21 at 20:03
  • I do not see one for `workbench.action.terminal.newWithCwd`. I dont see a way to add it either? do i even have a keybindings.json? – Omar Aug 08 '22 at 19:48
5

My solution was to simply add the following to VS Code Settings so that whatever key you have set for opening a new integrated terminal, it ensured that the terminal went to the directory of the currently opened file. This works regardless of the new terminal you select, be it powershell, CMD, Git Bash etc:

"terminal.integrated.cwd": "${fileDirname}"
Darren Evans
  • 665
  • 10
  • 17
  • Its already set that way by default and it works only after VS is loaded, first terminal is still in $HOME. – majkinetor Jun 23 '21 at 15:34
2

With Code Runner and Ctrl+Alt+N command you can have it solved by creating a batch file and passing the ${fileDirname} value to the PowerShell 6 e.g like this:

  1. Adding/changing those two lines in VSCode settings.json:

    "terminal.integrated.shell.windows": "C:\\Program Files\\PowerShell\\6\\pwsh.bat",
    "terminal.integrated.shellArgs.windows": ["${fileDirname}"]
    
  2. Having such a pwsh.bat saved in the C:\Program Files\PowerShell\6\ folder:

    @echo off
    set arg1=%1
    "C:\Program Files\PowerShell\6\pwsh.exe" -noexit -Command "cd '%arg1%'"
    

EDIT

Small improvement to the batch file code useful when no file is opened and we still want to open a new Terminal:

```
@echo off
set arg1=%1
if [%arg1%] == [${fileDirname}] set arg1=
"C:\Program Files\PowerShell\6\pwsh.exe" -noexit -Command "cd '%arg1%'"
```
olin000
  • 836
  • 1
  • 7
  • 10
1

As of at least v2022.6.1 of the Visual Studio Code PowerShell extension:

  • Darren Evans' helfpul answer shows how to customize the startup directory for general-purpose shells running in Visual Studio Code's integrated terminal.

  • To customize the startup dir. for the PIC (PowerShell Integrated Console) - a specialized shell that comes with the PowerShell extension - use its analogous setting:

    • Via the GUI:

      • Open the Settings GUI via Ctrl-,
      • Locate setting PowerShell: Cwd
      • Specify ${fileDirname} as the value.
    • Via the settings.json file:

      "powershell.cwd": "${fileDirname}"
      
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I know this is an old thread, sorry for necroing it, but gonna answer either way for those stumbling here from Google.

If you want new terminals to open in your currently selected directory, open VSCode settings JSON, and add this line (or replace the one with the same key):

"terminal.integrated.cwd": "./",

kuvasz
  • 1
  • 2