【问题标题】:VSCode Integrated Terminal Doesn't Load .bashrc or .bash_profileVSCode 集成终端不加载 .bashrc 或 .bash_profile
【发布时间】:2019-01-20 02:25:05
【问题描述】:

我有以下文件来处理 shell 配置:

#~/.bash_profile
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

#~/.bashrc
... configure shell

如果我使用 code 从命令行打开 VSCode,则每当我添加集成 shell 的新实例时都会加载我的 .bashrc

但是,如果我通过其图标打开 VSCode,则只会加载我的 .profile

如何确保我的 .bashrc 被加载?

我尝试了terminal.integrated.shellArgs.osx 设置的各种设置,但没有任何运气。

【问题讨论】:

  • 我在加载 bash_profile 而不是 bashrc 时遇到问题。删除 bash_profile 文件修复了它。

标签: bash shell visual-studio-code settings


【解决方案1】:

只需将 shell 参数添加到 VsCode settings.json 文件。

settings.json文件的路径如下:

Windows: C:\Users\<username>\AppData\Roaming\Code\User\settings.json`

Linux:   $HOME/.config/Code/User/settings.json

Mac:     {Please edit me}

添加以下内容之一:

"terminal.integrated.shellArgs.windows": ["-l"],

"terminal.integrated.shellArgs.linux": ["-l"],

"terminal.integrated.shellArgs.osx": ["-l"],

这将使用 login 参数启动您选择的 shell。这将因此执行任何设置的用户配置文件。

【讨论】:

  • 当您使用命令选项板访问此文件并插入此设置时,["-l"] 周围的这些括号会丢失。当我添加对我有用的括号时,现在 osx zsh shell 正在加载我常用的环境。
  • 为了让这个也开始在工作区目录中,我将它添加到我的 ~/.bash_profile 的末尾:[[ -n "${VSCODE_IPC_HOOK_CLI}" ]] &amp;&amp; cd "${OLDPWD}"
  • VSCode 已弃用 "terminal.integrated.shellArgs.osx" 以支持使用配置文件。我建议访客在下面查看我的答案。
【解决方案2】:

VSCode 已弃用 "terminal.integrated.shellArgs.osx" 以支持使用配置文件。这在 osx 中为 bash 提供了窍门。如果您不希望 bash 成为您的默认配置文件,请省略第一行:

  "terminal.integrated.defaultProfile.osx": "bash",
  "terminal.integrated.profiles.osx": {
    "bash": {
      "path": "bash",
      "args": ["-l"]
    }
  }

【讨论】:

    【解决方案3】:

    "terminal.integrated.shellArgs.osx": ["-l"] 已弃用。

    我个人想用.bash_profile,所以我用这个做了一个.bashrc

    if [ -f ~/.bashrc ]; then
       source ~/.bash_profile
    fi
    

    然后我不得不重新启动计算机(只是重新启动 VS 代码不起作用)。

    【讨论】:

    • 第一行应该是if [ -f ~/.bash_profile ]; then吗?从同一个文件中测试文件是否存在并没有真正意义
    • @tel 好点,很难说,我只知道这对我有用。
    • +1 表示完全重启计算机。没有什么对我有用,然后就这样了。我很想知道为什么这在注销并再次登录时不起作用。
    【解决方案4】:

    另一个对我有用的可能解决方案。 settings.json 文件(您可以在 File > Preferences > Settings > Features > terminal > Integrated > Automation Shell: Linux 中访问)有一个参数

        "terminal.integrated.inheritEnv": false
    

    默认设置为 false。将其更改为 true 可以解决我的问题。

    【讨论】:

    • 天哪,我一直在寻找这个。谢谢!
    • 这也给了我需要的提示。其实现在terminal.integrated.inheritEnv的默认值好像是true,见code.visualstudio.com/updates/…
    【解决方案5】:

    我在 Mac 上使用 Intellij Idea 终端时遇到了同样的问题,两者的解决方案是相同的。在设置中将集成终端的路径更改为“/bin/bash”。希望对您有所帮助。

    【讨论】:

      【解决方案6】:

      您也可以尝试以下方法:

      1 创建一个名为 /usr/local/bin/bash-login 的文件并添加:

      #!/bin/bash
      bash -l
      

      2 运行:

      chmod +x /usr/local/bin/bash-login 
      

      使其可执行。

      3 在您的 VSC 用户设置中添加

         { "terminal.integrated.shell.osx": "/usr/local/bin/bash-login" }
      

      https://github.com/Microsoft/vscode/issues/7263 描述了解决方案。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2019-08-02
        • 2023-03-11
        • 2019-07-01
        • 2016-03-25
        • 2021-11-01
        • 1970-01-01
        • 2017-10-16
        • 2014-09-21
        • 1970-01-01
        相关资源
        最近更新 更多