How do I get IntelliJ Terminal to work properly with Oh My Zsh?
Asked Answered
A

8

25

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

  • cannot find executables
  • cannot use version managers like pyenv, sdkman, rvm

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.

ref: https://github.com/robbyrussell/oh-my-zsh

Aromaticity answered 24/6, 2018 at 0:11 Comment(2)
Use --login --terminal (-li for short)Adan
@Adan is this a solution?Currish
A
26

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

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/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

and in IntelliJ:

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

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

  • /etc/zshenv
  • $ZDOTDIR/.zshenv
  • If a login shell:
    • /etc/zprofile
    • $ZDOTDIR/.zprofile
  • If an interactive shell:
    • /etc/zshrc
    • $ZDOTDIR/.zshrc
  • If a login shell:
    • /etc/zlogin
    • $ZDOTDIR/.zlogin

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

and this in IntelliJ

/etc/zshrc
/Users/starver/.zshrc

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

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

which man path_helper explains:

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.)

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.

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

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

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...

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

Note: Issue reported to JetBrains in https://youtrack.jetbrains.com/issue/IDEA-194488.

Aromaticity answered 24/6, 2018 at 0:11 Comment(3)
In Jetbrains terminal configuration, you cannot enter /bin/zsh --login; it has no effect. Have you tried to quote it with doubleqoutes? "/bin/zsh --login"Childbearing
Not really the point of the paragraph or the answer, but, using a quoted Shell path "/bin/zsh --login" produces an java.io.IOException: Exec_tty error:Unknown reason when you try to start a terminal.Aromaticity
I don't have the .zshrc file under /Applications/IntelliJ IDEA.app/Contents/plugins/terminal/.zshrc - macOS monterey MBP 2021 M1Achaemenid
L
34

In my case, using MacOSX, I just change the Shell Path Preferences -> Tools -> Terminal -> Application Settings -> Shell path

Replace /bin/sh with /bin/zsh

enter image description here

Don't forget to close all preexisting terminal windows after pointing Intelijj to zsh installation.

Lodgings answered 4/3, 2021 at 16:27 Comment(0)
A
26

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

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/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

and in IntelliJ:

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

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

  • /etc/zshenv
  • $ZDOTDIR/.zshenv
  • If a login shell:
    • /etc/zprofile
    • $ZDOTDIR/.zprofile
  • If an interactive shell:
    • /etc/zshrc
    • $ZDOTDIR/.zshrc
  • If a login shell:
    • /etc/zlogin
    • $ZDOTDIR/.zlogin

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

and this in IntelliJ

/etc/zshrc
/Users/starver/.zshrc

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

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

which man path_helper explains:

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.)

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.

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

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

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...

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

Note: Issue reported to JetBrains in https://youtrack.jetbrains.com/issue/IDEA-194488.

Aromaticity answered 24/6, 2018 at 0:11 Comment(3)
In Jetbrains terminal configuration, you cannot enter /bin/zsh --login; it has no effect. Have you tried to quote it with doubleqoutes? "/bin/zsh --login"Childbearing
Not really the point of the paragraph or the answer, but, using a quoted Shell path "/bin/zsh --login" produces an java.io.IOException: Exec_tty error:Unknown reason when you try to start a terminal.Aromaticity
I don't have the .zshrc file under /Applications/IntelliJ IDEA.app/Contents/plugins/terminal/.zshrc - macOS monterey MBP 2021 M1Achaemenid
A
8
zsh --login --interactive

Worked for me as terminal command, or for short:

zsh -li

This will load /etc/zprofile in Mac and all login scripts.

Adan answered 29/1, 2019 at 8:45 Comment(2)
I had no access to my zsh history. After running this inside the intellij terminal, I was able to do so.Hathor
This should be the accepted answerSixteenth
E
7

Try to uncomment first string in ~/.zshrc:

# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:$PATH
Eberle answered 21/9, 2018 at 12:48 Comment(0)
M
2

I can confirm everything in this answer, but there is a simpler workaround based on the fact, that loading path_helper twice doesn't matters.

So until JetBrains fixes their Terminal plugin, just put source /etc/zprofile to your ~/.zshrc file and profit!

Masochism answered 24/10, 2018 at 15:23 Comment(0)
T
2

Nothing worked from the replies here in Fedora 32 with zoppo over zsh :/

configuration files are loaded but it's like there's lack of some of them (I'm sorry for being not so detailed), including them manually didn't work too... my ~/.zshrc is empty: all the configurations are globals in /etc/{zshenv, zprofile, zshrc}.

The only working solution was, for me, to use

sh -c zsh

as "Shell path". Then it works as in konsole and others terminal emulators.

Tasty answered 14/11, 2020 at 7:34 Comment(0)
A
-1

To fix sdkman with zsh, just execute this line: works fine for me Ubuntu 19.04

echo 'source "$HOME/.sdkman/bin/sdkman-init.sh"' >> ~/.zshrc
Ammoniate answered 5/2, 2020 at 23:12 Comment(0)
B
-1

I updated the "Shell path" from /bin/zsh to /usr/local/bin/zsh and it's now working.

enter image description here

Explanation

I'm not sure if this is a proper solution, but it works for me and it's the simplest of all.

Before this change, /usr/local/bin is removed from my $PATH after starting virtual env. And when I saw that the shell path points to /bin/zsh this random thought came to me that maybe I should try pointing to /usr/local/bin/zsh instead -- I don't know anything about how this works lol but then somehow it works. Now /usr/local/bin is no longer removed from my $PATH.

Btw this issues started to happen to me after I upgrade PyCharm to 2023.2.5 (Build #PY-232.10227.11)

Breger answered 23/11, 2023 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.