在脚本文件夹中打开 Visual Studio Code Powershell 终端

3
当我在VSCode中右键单击一个ps1脚本并在PowerShell终端中打开它时,当前文件夹不是脚本文件夹而是项目文件夹。有没有办法配置它以在脚本文件夹中打开?
1个回答

4

假设:

  • 你已经将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\"'" ]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接