Configure common command-line tools on Mac OS X January 29, 2009

Change your default shell to zsh

By default, your Mac uses bash as the default shell. Well, it can be a good shell, but I am much more used to zsh, for which I have some good configuration files. Fortunately, zsh is included in Mac OS X Snow Leopard, so switching to it won’t be too difficult.

Simply open System Preferences, and then go to the Accounts preferences panel. If needed, click on the lock (at the bottom left of the panel) to make the changes we need; you will have to enter your password. Then, in the list of the available accounts, right click on your account, and click on Advanced Options. There, just change the Login Shell to /bin/zsh. Be careful not to change anything else, since it could damage your account. Then click on OK, and click again on the lock to prevent further changes.

That’s it, you can close the System Preferences window. Close your Terminal window, and open a new one; you may not notice it, but you are in zsh now, instead of bash. But it still needs some configuration to get something really usable…

Configure zsh

In your new terminal window, make sure you have root‘s privileges by typing:

sudo -s

and do the following:

cd /var/root
rm -f /etc/zlogin /etc/zlogout /etc/zshenv /etc/zshrc
mkdir /etc/zsh
cp command-line-tools-configuration/etc/z* /etc/zsh
cd /etc && for file in /etc/zsh/z*; do ln -s "$file"; done && cd /var/root
cp command-line-tools-configuration/root/zlogin .zlogin
cp command-line-tools-configuration/root/zshrc .zshrc
sed -i.bak "s/joel/$SUDO_USER/g" .zshrc && rm -f .zshrc.bak
cp command-line-tools-configuration/home/zlogin ~$SUDO_USER/.zlogin
cp command-line-tools-configuration/home/zshrc ~$SUDO_USER/.zshrc
chown "$SUDO_USER":staff ~$SUDO_USER/.zlogin ~$SUDO_USER/.zshrc

And we’re done! Let’s just clean the configuration files:

rm -R -f /var/root/command-line-tools-configuration
rm -f /var/root/command-line-tools-configuration.tar.bz2

Now, close your Terminal window and open a new one, and… Yes, you can just say “Wow! Didn’t believe I could have that on my Mac!”

Let me just highlight here the most handy aliases you will get with this new shell configuration:

  • man will now use vim transformed as a pager to get colors within your man pages;
  • ls and grep have more beautiful output (colors, and stuff);
  • cp, mv and rm are now interactive by default, to prevent unintended actions;
  • df and du output will be human readable by default;
  • ll stands for ls -l, lla stands for ls -l -a, md stands for mkdir, rd stands for rmdir, s stands for cd .., c stands for clear, etc.; see /etc/zsh/zshrc for more details;
  • su will now do the same thing as sudo -s, except that environment variables will be tweaked on the fly to give you the impression you actually used su instead of sudo; this way, you can gain root privileges like you would do with most Linux distributions, without actually activating the root account on Mac OS X, which is cool; but you can always use sudo like you did before, if you prefer;
  • when you are root (after using su, for example), you can type upgrade instead of port selfupdate && port upgrade outdated to upgrade your MacPorts install as well as all the packages installed through MacPorts; it will also automatically clean your MacPorts install by uninstalling every package which has been deactivated (which typically happens when a new version of a package is released).

You will also get a beautiful prompt which changes color according to the type of user (root is blue, and others are red), as well as great options for zsh, such as advanced history management, and an advanced auto-completion scheme.

One more thing: go grab mmv for Mac OS X, and just enjoy!

Page: 1 2 3Next >

One Comments
Romero January 31st, 2009

Hiding /opt created within the install process of MacPorts

Thanks for the tutorial! (still having some customizing problem with figlet, but the rest is fine!)

For my concern, the /opt folder wasn’t there before MacPorts install (I don’t know if it’s the only one to use it?).

In order to “clean” the all thing, I found two interesting issues:

1- I don’t like to see the whole content of /Developer and /opt in the Spotlight search. So I first recommend to get these two in the excluding list of Spotlight (System Preferences).

2- I looked a little bit to know how to hide /opt like /usr, /etc are.
In an OS X way, it’s not that bad! :-). Obviously, putting a . in front of the name is no solution… At all.

You’ll need the Developer Tools, but you are supposed to install them anyway if you want to use MacPorts.

In the of course, use the brand ‘new’ root user:

root@machine ~# SetFile -a V /opt

Basically, it’s switching a bit in the file, responsible for (in)visibility in the Finder. Usually, you have to reload the Finder, but I didn’t have to.

It’s nicer 🙂

Leave a Reply