3

I'm using [oh-my-zsh] in iTerm on MacBook running Mavericks.

I have iTerm set to source my .zsh file on load, and mostly that works.

cd && source .zsh

Except when it doesn't. Sometimes, even in the middle of a session, it will forget my environment vars and aliases. If that happens, even basic stuff like mvn gets "forgotten."

I'm not sure if this is an issue with iTerm, zsh, or oh-my-zsh. Any thoughts?

1
  • 1
    The configuration you describe doesn't make sense. Why would you load a file manually? What does it mean for the terminal to load a .zsh file — the shell running in the terminal is the one that loads things like aliases. In any case, it's difficult to help you with an imprecisely described problem that happens “sometimes”. At least post the content of your zsh configuration files (~/.zshrc, ~/.zshenv, ~/.zprofile, ~/.zlogin) as well as this .zsh file and any other file that they include and that you've customized. Apr 17 '15 at 22:01
4

According to the zsh user guide, aliases should be defined in ~/.zshrc:

You may be able to think of some aliases you want to define in your startup files; .zshrc is probably the right place.

It also has a tip for keeping your ~/.zshrc clean:

I only tend to use aliases in interactive shells, so I define them from .zshrc, but you may want to use .zshenv if you use aliases more widely. In fact, to keep my .zshrc neat I save all the aliases in a separate file called .aliasrc and in .zshrc I have:

if [[ -r ~/.aliasrc ]]; then
   . ~/.aliasrc   
fi

which checks if there is a readable file ~/.aliasrc, and if there is, it runs it in exactly the same way the normal startup files are run.

So, you might want to create a file called ~/.aliasrc and source it (. means source) from your ~/.zshrc.

The same source suggests that environmental variables should be in ~/.zshenv:

The easiest place to put these is in .zshenv --- hence it's name. Environment variables will be passed to any programmes run from a shell, so it may be enough to define them in .zlogin or .zprofile: however, any shell started for you non-interactively won't run those, and there are other possible problems if you use a windowing system which is started by a shell other than zsh or which doesn't run a shell start-up file at all --- I had to tweak mine to make it do so. So .zshenv is the safest place; it doesn't take long to define environment variables. Other people will no doubt give you completely contradictory views, but that's people for you.

0
1

Place your environment variables in your ~/.zshrc file, they will get sourced automatically on each zsh session.

3
  • +1, though I was advised not to edit the .zshrc file. But you're saying it's OK?
    – Robusto
    Apr 17 '15 at 10:57
  • @Robusto, your configuration usually goes in ~/.zshrc so yeah, it's fine to edit it
    – zer0rest
    Apr 17 '15 at 11:04
  • 2
    Environment variables belong in ~/.profile or ~/.zprofile, not ~/.zshrc, otherwise they won't be available in programs that are not started from a terminal. Apr 17 '15 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.