1

I am running the PowerShell extension in Visual Studio Code with its PowerShell Integrated Console attached. I have two PowerShell scripts each open in separate tabs. The files are located in different directories.

I would like to quickly change the current directory to the currently active tab in the PowerShell console using a keyboard shortcut (e.g. Ctrl+Alt+D).

How does one accomplish this?

Gama11
  • 31,714
  • 9
  • 78
  • 100
skataben
  • 2,023
  • 1
  • 22
  • 25
  • 1
    Did you see this: https://stackoverflow.com/questions/56218958/how-to-quickly-change-shell-folder-to-match-the-currently-open-file/56222855#56222855 and does it work for you? – Mark Sep 19 '19 at 19:43
  • @Mark I did not see that, and with a small tweak it works great in the PowerShell console as well as cmd. It needs the /d switch in case the drive letter changes, and it needs escaped double quotes for cmd. {"text": "cd /d \"${fileDirname}\"\u000D"} How do I give you credit for a better answer? – skataben Sep 19 '19 at 20:04
  • Scratch the "/d" parameter. It doesn't work in the PowerShell console. {"text": "cd \"${fileDirname}\"\u000D"} I upvoted the answer you linked. – skataben Sep 19 '19 at 20:12
  • In a normal cmd console (not PowerShell), if you are in the C:\ and you type "cd Z:\" it does *not* change the current directory. If you add the /d parameter, it does. Unfortunately, the /d parameter throws an error in the PowerShell console, so I removed it. – skataben Sep 19 '19 at 20:20
  • Since it is largely a duplicate question I am happy with an upvote to the original answer. You should edit your answer below (and probably the github issue) to add the new changes that you discovered - and I will edit my answer to link to your answer then). I understand your tweaks now - I tested it in Powershell and bash, but not changing to a different drive directory altogether. – Mark Sep 19 '19 at 20:21

1 Answers1

1

UPDATE: This is the best solution so far

(Thanks to Mark in the comments of the original post.)

In the keybindings.json file, add the following key binding:

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

This works in both cmd and PowerShell consoles. (The only flaw is that it does not change the directory across drives for the cmd console only).


UPDATE 2:

A new command will be added in v1.39 workbench.action.terminal.newWithCwd, see add terminal here command. The example keybinding given is:

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

This will create a new terminal, not change the current terminal.


Previous answer

Credit goes to SeeminglyScience for this answer.

There is not currently a way to set a keyboard shortcut, but since that is not explicitly stated in the title, I will post another shortcut.

  1. If you do not already have a file called Microsoft.VSCode_profile.ps1 in your "%UserProfile%\Documents\WindowsPowerShell" directory, create one.

  2. Create a custom PowerShell editor command by adding the following to the Microsoft.VSCode_profile.ps1 file:

    Register-EditorCommand -Name 'ChangeDirHere' -DisplayName 'Change to the Directory of the Current File' -ScriptBlock {
        Set-Location ([System.IO.Path]::GetDirectoryName($psEditor.GetEditorContext().CurrentFile.Path))
    }
    
  3. Restart VS Code.

  4. Open any PowerShell file.
  5. Press Alt+Shift+S (or whatever keybinding you have for "Powershell: Show Additional Commands from PowerShell Modules").
  6. Select the custom command from the drop-down list of PowerShell commands.
Mark
  • 143,421
  • 24
  • 428
  • 436
skataben
  • 2,023
  • 1
  • 22
  • 25
  • See https://stackoverflow.com/questions/56218958/how-to-quickly-change-shell-folder-to-match-the-currently-open-file/56222855#56222855 for an update. A new command is being added that will make this even easier. – Mark Sep 24 '19 at 04:00