简体   繁体   中英

Open Visual Studio Code Powershell Terminal in script folder

When I right-click a ps1 script in VSCode and open it in Powershell Terminal, the current folder is not the script folder but the project folder.

Is there a way to configure it to open in the script folder ?

Assumptions:

  • You've configured PowerShell as the default shell - either via command Terminal: Select Default Shell (on Windows) or directly via user settings "terminal.integrated.shell.windows" / "terminal.integrated.shell.osx" / "terminal.integrated.shell.linux" .

  • You're trying to open a PowerShell instance by right-clicking a file / subfolder in the side bar's Explorer view and choose Open in Terminal , and you want that instance's current location to be that file's containing folder / that subfolder .

As of (at least) VSCode 1.17.2, this should work by default .

If it doesn't, perhaps custom code in your $PROFILE directly or indirectly changes the current location .

If you don't want to / cannot prevent $PROFILE code from changing the location, here's a workaround via the user settings that explicitly sets the current location after the $PROFILE code has run:

On Windows :

"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.shellArgs.windows": [ "/c", "powershell -noexit -command 'Set-location \"%CD%\"'" ]

This uses cmd.exe as the default shell, which then invokes PowerShell with a command that explicitly makes it change to that folder.

The workarounds for other platforms below work analogously:

On macOS :

"terminal.integrated.shell.osx": "/bin/bash",
"terminal.integrated.shellArgs.osx": [ "-l", "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]

On Linux :

"terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": [ "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM