【发布时间】:2017-11-04 18:32:05
【问题描述】:
当我在 VSCode 中右键单击一个 ps1 脚本并在 Powershell 终端中打开它时,当前文件夹不是脚本文件夹而是项目文件夹。
有没有办法将其配置为在脚本文件夹中打开?
【问题讨论】:
标签: powershell visual-studio-code
当我在 VSCode 中右键单击一个 ps1 脚本并在 Powershell 终端中打开它时,当前文件夹不是脚本文件夹而是项目文件夹。
有没有办法将其配置为在脚本文件夹中打开?
【问题讨论】:
标签: powershell visual-studio-code
假设:
您已将 PowerShell 配置为默认 shell - 通过命令 Terminal: Select Default Shell(在 Windows 上)或直接通过用户设置 "terminal.integrated.shell.windows" / "terminal.integrated.shell.osx" / "terminal.integrated.shell.linux"。
您正试图通过 右键单击侧栏的 Explorer 视图中的文件/子文件夹并选择 Open in Terminal 来打开 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\"'" ]
【讨论】: