当我在VSCode中右键单击一个ps1脚本并在PowerShell终端中打开它时,当前文件夹不是脚本文件夹而是项目文件夹。有没有办法配置它以在脚本文件夹中打开?
假设:
你已经将PowerShell配置为默认shell - 通过命令终端:选择默认shell
(在Windows上)或直接通过用户设置"terminal.integrated.shell.windows"
/"terminal.integrated.shell.osx"
/"terminal.integrated.shell.linux"
。
你正在尝试通过在侧边栏的资源管理器
视图中右键单击文件/子文件夹并选择在终端中打开
来打开PowerShell实例,并且你希望该实例的当前位置是该文件所在的文件夹/该子文件夹。
至少在VSCode 1.17.2版本中,这应该默认工作。
如果不行,也许你的$PROFILE
中的自定义代码直接或间接地改变了当前位置。
如果你不想/不能阻止$PROFILE
代码改变位置,那么可以通过用户设置的解决方法,在$PROFILE
代码运行后明确设置当前位置:
在Windows上:
"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.shellArgs.windows": [ "/c", "powershell -noexit -command 'Set-location \"%CD%\"'" ]
这里默认使用cmd.exe
作为默认 shell ,然后通过一个命令显式地将其更改到该文件夹并调用 PowerShell。
以下是其他平台的解决方法:
在 macOS 上:
"terminal.integrated.shell.osx": "/bin/bash",
"terminal.integrated.shellArgs.osx": [ "-l", "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]
在Linux系统上:
"terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": [ "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]