【问题标题】:How do I get IntelliJ Terminal to work properly with Oh My Zsh?如何让 IntelliJ 终端与 Oh My Zsh 一起正常工作?
【发布时间】:2018-12-02 23:56:58
【问题描述】:

我喜欢 Oh My Zsh,但它从未在 JetBrains 产品的终端中正常工作:

  • 找不到可执行文件
  • 无法使用 pyenv、sdkman、rvm 等版本管理器

哦,我的 Zsh 是 zsh shell 增强,所以实际问题可以简化为让 zsh 正常工作。在阅读了一些 intellij 问题后,我尝试切换所有终端配置选项(单独和集体)。

参考:https://github.com/robbyrussell/oh-my-zsh

【问题讨论】:

  • 使用--login --terminal(简称-li

标签: intellij-idea pycharm zsh oh-my-zsh goland


【解决方案1】:

找不到二进制文件,无法运行东西?显然是 $PATH 的问题,但是是什么以及为什么?

我在 iTerm2 中回显了一条已知的好路径

/Users/starver/.sdkman/candidates/maven/current/bin:/Users/starver/.sdkman/candidates/groovy/current/bin:/Users/starver/.sdkman/candidates/gradle/current/bin:/usr/local/Cellar/pyenv-virtualenv/1.1.3/shims:/Users/starver/.pyenv/shims:/Users/starver/.pyenv/bin:/Users/starver/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/opt/X11/bin:/usr/local/git/bin:/Users/starver/bin/:/Users/starver/code/go/bin/:/Users/starver/.rvm/bin

在 IntelliJ 中:

/usr/bin:/bin:/usr/sbin:/sbin

这暗示了启动文件加载问题。我的 zsh 手册页说加载顺序应该是:

  • /etc/zshenv
  • $ZDOTDIR/.zshenv
  • 如果是登录外壳:
    • /etc/zprofile
    • $ZDOTDIR/.zprofile
  • 如果是交互式外壳:
    • /etc/zshrc
    • $ZDOTDIR/.zshrc
  • 如果是登录外壳:
    • /etc/zlogin
    • $ZDOTDIR/.zlogin

在为每个存在的文件添加echo 后,我得到了 iTerm2 的以下内容:

/etc/zprofile
/Users/starver/.zprofile
/etc/zshrc
/Users/starver/.zshrc
/Users/starver/.zlogin

这在 IntelliJ 中

/etc/zshrc
/Users/starver/.zshrc

IntelliJ 认为这不是登录 shell。在Jetbrains终端配置中,不能输入/bin/zsh --login;它没有效果。玩了一会,发现打开Tools -> Terminal -> Shell Integration 让终端成为“登录shell”,启动文件加载故事也有所改善:

/etc/zshrc
/Users/starver/.zprofile
/Users/starver/.zshrc
/Users/starver/.zlogin

请注意,没有任何全局 zsh 启动文件,这是根本问题:/etc/zprofile 包含:

# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

哪个 man path_helper 解释:

path_helper 实用程序读取目录 /etc/paths.d 和 /etc/manpaths.d 中文件的内容,并将它们的内容分别附加到 PATH 和 MANPATH 环境变量中。 (MANPATH 环境变量不会被修改,除非它已经在环境中设置。)

在 shell 启动期间至少执行一次 path_helper 非常重要:pathspaths.d 是系统和第三方安装程序定义其路径添加的地方。不执行系统配置文件启动文件是为什么/usr/local/bin/usr/local/go等不在路径上。

我尝试了几种方法,寻找一个优雅的解决方案。显然,jediterm 终端实现阻止挂钩到标准终端启动过程 - 因此它们在/Applications/IntelliJ IDEA.app/Contents/plugins/terminal/.zshrc 中实现启动文件加载。我们可以修复那个实现!!将该文件替换为:

#!/bin/zsh

# starver mod
# Jetbrains uses jediterm as a java terminal emulator for all terminal uses.
# There are some apparent limits on use:
# - must use old-style shebang - not the #!/usr/bin/env zsh
# - must implement the startup file loading here
#
# Note: original contents are in lib/terminal.jar

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
bindkey '^[^[[C' forward-word
bindkey '^[^[[D' backward-word

ZDOTDIR=$_OLD_ZDOTDIR

if [ -n "$JEDITERM_USER_RCFILE" ]
then
  source "$JEDITERM_USER_RCFILE"
  unset JEDITERM_USER_RCFILE
fi

if [ -n "$ZDOTDIR" ]
then
  DOTDIR=$ZDOTDIR
else
  DOTDIR=$HOME
fi

if [ -f "/etc/zshenv" ]; then
     source "/etc/zshenv"
fi

if [ -f "$DOTDIR/.zshenv" ]; then
     source "$DOTDIR/.zshenv"
fi

if [ -n $LOGIN_SHELL ]; then
  if [ -f "/etc/zprofile" ]; then
       source "/etc/zprofile"
  fi
  if [ -f "$DOTDIR/.zprofile" ]; then
       source "$DOTDIR/.zprofile"
  fi
fi

if [ -f "/etc/zshrc" ]; then
     source "/etc/zshrc"
fi

if [ -f "$DOTDIR/.zshrc" ]; then
     source "$DOTDIR/.zshrc"
fi

if [ -n $LOGIN_SHELL ]; then
  if [ -f "/etc/zlogin" ]; then
       source "/etc/zlogin"
  fi
  if [ -f "$DOTDIR/.zlogin" ]; then
       source "$DOTDIR/.zlogin"
  fi
fi

if [ -n "$JEDITERM_SOURCE" ]
then
  source $(echo $JEDITERM_SOURCE)
  unset JEDITERM_SOURCE
fi

现在,在 IntelliJ 终端启动时,我看到了

/etc/zshrc
/etc/zprofile
/Users/starver/.zprofile
/Users/starver/.shell-common
/etc/zshrc
/Users/starver/.zshrc
/Users/starver/.zlogin

第一个/etc/zshrc 在插件的.zshrc 之前执行,对此我无能为力,也不会造成任何不良副作用...

对每个 JetBrains 产品重复该过程,您就可以随时随地享受 Oh My Zsh 的乐趣。

注意:问题已在https://youtrack.jetbrains.com/issue/IDEA-194488 中报告给 JetBrains。

【讨论】:

  • 在Jetbrains终端配置中,无法输入/bin/zsh --login;它没有任何效果。 你试过用双引号引用它吗? "/bin/zsh --login"
  • 不是段落的重点或答案,但是,当您尝试启动终端时,使用带引号的 Shell 路径“/bin/zsh --login”会产生java.io.IOException: Exec_tty error:Unknown reason。跨度>
【解决方案2】:

尝试取消注释~/.zshrc中的第一个字符串:

# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:$PATH

它帮助了我。

【讨论】:

    【解决方案3】:

    我可以确认所有in this answer,但基于事实有一个更简单的解决方法,加载path_helper 两次并不重要。

    所以在 JetBrains 修复他们的终端插件之前,只需将 source /etc/zprofile 放入您的 ~/.zshrc 文件并获利!

    【讨论】:

      【解决方案4】:
      zsh --login --interactive
      

      terminal command 为我工作,或简称:

      zsh -li
      

      这将加载 Mac 中的 /etc/zprofile 和所有登录脚本。

      【讨论】:

      • 我无法访问我的 zsh 历史记录。在 intellij 终端中运行此程序后,我能够这样做。
      【解决方案5】:

      要使用 zsh 修复 sdkman,只需执行以下行:适用于我 Ubuntu 19.04

      echo 'source "$HOME/.sdkman/bin/sdkman-init.sh"' >> ~/.zshrc
      

      【讨论】:

        【解决方案6】:

        在 Fedora 32 中使用 zoppo over zsh 的回复没有任何效果:/

        配置文件已加载,但好像缺少其中一些(很抱歉没有这么详细),包括它们手动也不起作用......我的~/.zshrc 是空的:所有配置都是/etc/{zshenv, zprofile, zshrc} 中的全局变量。

        对我来说,唯一可行的解​​决方案是使用

        sh -c zsh
        

        作为“Shell 路径”。然后它就像在 konsole 和其他终端仿真器中一样工作。

        【讨论】:

          【解决方案7】:

          在我的情况下,使用 MacOSX,我只需更改 Shell 路径 Preferences -> Tools -> Terminal -> Application Settings -> Shell path

          /bin/sh 替换为/bin/zsh

          【讨论】:

            猜你喜欢
            • 2018-12-02
            • 2011-10-11
            • 2017-05-02
            • 2018-08-23
            • 2021-05-07
            • 1970-01-01
            • 2013-10-09
            • 1970-01-01
            • 2015-12-23
            相关资源
            最近更新 更多