如何让 IntelliJ 终端与 Oh My Zsh 一起正常工作? [英] How do I get IntelliJ Terminal to work properly with Oh My Zsh?

查看:17
本文介绍了如何让 IntelliJ 终端与 Oh My Zsh 一起正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I love Oh My Zsh, but it has never worked properly in the JetBrains product's Terminals:

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

哦,我的 Zsh 是 zsh shell 增强,所以实际问题可以减少到只是让 zsh 正常工作.在阅读了一些 Intellij 问题后,我尝试切换所有终端配置选项(单独和整体),以取得成功.

Oh My Zsh is zsh shell augmentation, so the actual problem could be reduced to just getting zsh to work properly. I have tried toggling all of the Terminal config options (individually and en masse) after reading some intellij issues, to an avail.

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

推荐答案

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

Can't find binaries, can't run stuff? Obviously a $PATH problem, but what and why?

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

I echo'd a known good path in iTerm2

/Users/starver/.sdkman/candidates/maven/current/bin:/Users/starver/.sdkman/candidates/groovy/current/bin:/Users/starver/.sdkman/candidates/gradle/当前/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 中:

and in IntelliJ:

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

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

This hints at a startup file loading problem. My zsh man page says the load order should be:

  • /etc/zshenv
  • $ZDOTDIR/.zshenv
  • 如果是登录 shell:
    • /etc/zprofile
    • $ZDOTDIR/.zprofile
    • /etc/zshrc
    • $ZDOTDIR/.zshrc
    • /etc/zlogin
    • $ZDOTDIR/.zlogin

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

    After adding an echo to each of those files that existed, I got the following for iTerm2:

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

    这在 IntelliJ 中

    and this in IntelliJ

    /etc/zshrc
    /Users/starver/.zshrc
    

    IntelliJ 认为这不是登录 shell.在Jetbrains终端配置中,不能输入/bin/zsh --login;它没有效果.玩了一会儿,发现打开工具->终端->Shell集成,让终端变成了登录shell",启动文件加载的故事也改善了一点:

    IntelliJ thinks this is not a login shell. In Jetbrains terminal configuration, you cannot enter /bin/zsh --login; it has no effect. After playing a bit, I found that turning on Tools -> Terminal -> Shell Integration makes the terminal a "login shell" and the startup file load story improved a bit:

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

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

    Notice that none of the global zsh startup files and this is the root problem: /etc/zprofile contains:

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

    path_helper 的解释:

    which man path_helper explains:

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

    The path_helper utility reads the contents of the files in the directories /etc/paths.d and /etc/manpaths.d and appends their contents to the PATH and MANPATH environment variables respectively. (The MANPATH environment variable will not be modified unless it is already set in the environment.)

    在 shell 启动期间至少执行一次 path_helper 非常重要:pathspaths.d 是系统和第三方安装程序定义的地方他们的路径添加.不执行system profile启动文件就是/usr/local/bin/usr/local/go等不在路径上的原因.

    Executing path_helper at least once during shell startup is REALLY important: paths and paths.d are where the system and third party installers define their path additions. Not executing the system profile startup file is why /usr/local/bin, /usr/local/go, etc. are not on the path.

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

    I tried several approaches, looking for an elegant solution. Apparently, the jediterm terminal implementation prevents hooking into a standard terminal startup process - so they implement startup file loading in /Applications/IntelliJ IDEA.app/Contents/plugins/terminal/.zshrc. We can fix that implementation!! Replace that file with:

    #!/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 终端启动时,我看到

    Now, on IntelliJ terminal startup, I see

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

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

    The first /etc/zshrc is executed before the plugin's .zshrc, nothing I can do about that, and it is not causing any bad side effects...

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

    Repeat the process for every JetBrains product and you can have the joy that is Oh My Zsh everywhere.

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

    这篇关于如何让 IntelliJ 终端与 Oh My Zsh 一起正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆