22

I want to use emacs from Applications folder when I'm using Mac, but I'm using same .zshrc in Ubuntu.

alias emacs='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'

So I want to create this alias for only when I'm using OS X. How can I get a OS name in .zshrc?

ironsand
  • 5,205

3 Answers3

28

I also share my Zsh startup between multiple operating systems. You could use a case statement for those commands which are system-specific:

case `uname` in
  Darwin)
    # commands for OS X go here
  ;;
  Linux)
    # commands for Linux go here
  ;;
  FreeBSD)
    # commands for FreeBSD go here
  ;;
esac

Alternatively you can split off system-specific startup into files called (say) .zshrc-Darwin, .zshrc-Linux, etc., and then source the required one near the end of your .zshrc:

source "${ZDOTDIR:-${HOME}}/.zshrc-`uname`"
wjv
  • 1,231
16

Checking environment variable $OSTYPE is preferred, cause it's more lightweighted compared with running the external command uname.

OSTYPE is set by ZSH the shell itself.

OSTYPE

The operating system, as determined at compile time.

http://zsh.sourceforge.net/Doc/Release/Parameters.html#Parameters-Set-By-The-Shell

Example

# for ZSH
case "$OSTYPE" in
  darwin*)
    # ...
  ;;
  linux*)
    # ...
  ;;
  dragonfly*|freebsd*|netbsd*|openbsd*)
    # ...
  ;;
esac

Reference

Note: Available OSTYPE values in Bash are a little different with the values in ZSH.

Simba
  • 1,682
3

Just check if you are not running linux. If uname does not exist in Mac, the if clause will fail too.

if [ "$(uname 2> /dev/null)" != "Linux" ]; then
    alias emacs='vim'
fi
Kira
  • 4,807