如何配置VS Code SSH远程连接的不同Shell?

16
3个回答

19

在 @Matt Bierner 的回答中补充一点。

较新版本的 vscode 现在可以让您为终端设置配置文件,并给它们自定义名称,该名称应在您的 远程设置 中引用。

CTRL+SHIFT+P -> 首选项: 打开设置 (JSON)

用户配置

...
"terminal.integrated.profiles.linux": {
    "s-mann-term": {
        "path": "/usr/bin/zsh"
    },
    "bash": {
        "path": "bash"
    },
    "zsh": {
        "path": "zsh"
    },
    "my-fav-term": {
        "path": "fish"
    }
},
"terminal.integrated.defaultProfile.linux": "s-mann-term"
...

这将使所有主机都默认使用/usr/bin/zsh(我只是在我的配置文件中使用了path关键字,但是您可以修改许多其他选项)。

注意:您也可以为同一shell添加多个配置文件。例如,5个不同配置的zsh配置文件。

CTRL+SHIFT+P -> Preferences: Open Remote Settings (SSH: az-box1)

az-box1配置

...
"terminal.integrated.defaultProfile.linux": "my-fav-term"
...

但是az-box1将默认为fish


2
对于较新版本的VSCode,这个答案是正确的!谢谢。 - Dvir Itzko
你能解释一下为什么在s-mann-term中使用/usr/bin/zsh,而在zsh配置文件中只使用zsh吗? - j7skov
我之所以问这个问题,是因为如果我在我的远程settings.json中使用terminal.integrated.shell.linux,它可以工作,但是上面的解决方案却不行。 - j7skov

13

您可以使用远程设置来更改每个主机的 shell。要执行此操作,请在 VS Code 中打开远程工作区,并运行 Open Remote settings 命令:

打开远程设置命令

terminal.integrated.shell.linux 设置为指向您的 shell 并保存文件:

"terminal.integrated.shell.linux": "/usr/bin/fish"

远程设置适用于您在给定主机上打开的所有工作空间,但必须针对每个您连接到的主机进行配置。

输入图片描述


4
这已经被弃用了。新的功能设置正在使用 terminal.integrated.profiles.linux - thenakulchawla

1

以上的答案都对我无效,我已经尝试了很多个月将默认的shell设置为zsh。最终有效的方法是在我的.bashrc文件顶部添加以下内容:

if [[ "$TERM_PROGRAM" == "vscode" ]]; then
  # ~/.profile is run by the login shell (this is what ssh uses)
  # ~/.bashrc is run by the interactive shell (this is what vscode uses)
  # Therefore, we only need to change the shell to zsh here since
  # vscode will run ~/.bashrc for us.
  exec zsh -l
fi

TERM_PROGRAM是从哪里来的?当连接到运行OpenSSH服务且其shell设置为C:/program files/git/bin/bash.exe的Windows机器时,我没有看到它被设置。 - jozxyqk

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