Most people never notice the Terminal application that comes with OS X. It just sits in the Utilities folder, like Megatron beneath the ice of the Arctic (Transformers reference anyone?) For those of us with the need to traverse the text, every tweak helps. And so I offer these tips that I wish somebody offered me when I was first starting out.
Most of these tweaks are made in a file named .bash_login that can be found in your home directory. This file is used to set certain preferences every time a new Terminal window is loaded. This means that in order to view changes brought about by modifying .bash_login, you need to open a new Terminal window.
Since .bash_login is an invisible file, it’s easiest to open through the command line. Since I’m a TextMate guy, and I opted to install the shell command, I can open bash login using the mate ~/.bash_login command. If you don’t know how to open a file in your favorite text editor via the command line, you can use one of the built-in text editors. I’d recommend nano, which you can use by running this command: nano ~/.bash_login
So now you know how to edit your .bash_login file. Now let’s take a look at why that’s so cool.
A Terminal without ANSI colors is like a Documents folder without icons. Being able to easily distinguish between directories and files is something is essential to quick and easy file browsing via the command line. To enable ANSI colors, add the following lines to your .bash_login file:
export CLICOLOR=1 export LSCOLORS=ExFxCxDxBxegedabagacad
Then quit and reload a new Terminal session. Now, whenever you run the “ls” command, you ought to notice some useful coloring to help guide the way.
One thing that always bothered me about using the command line was that the prompt was always a gargantuan string that consisted of my computer name, followed by my username, followed by my working directory. It’s obnoxious when your Terminal’s command prompt takes up an entire line of its own. So I changed it to just contain my working directory and whatever user is logged in at that moment. How? Add the following line to your .bash_login:
export PS1="[\w] \u$ "
See this page for more ways to customize your prompt.
Free, open-source software is all around. While most Linux users have package management systems to pluck whatever they need, whenever they need it, OS X doesn’t come with one. That’s where “MacPorts”: http://www.macports.org/ comes in. MacPorts gives you the “port” command, which can be used to install open source packages with ease. There’s also another project called “Fink”: http://finkproject.org/ which promises the same as MacPorts, though I’ve found MacPorts to be easier to use.
This one is a bit more advanced, but it’s worth it. “Cheat”: http://cheat.errtheblog.com/ has over a hundred cheat sheets about a wide variety of topics that are almost certainly interesting to any reader who has made it this far in this post. To get cheat, you first need to install Ruby and RubyGems, which shouldn’t be too difficult considering the fact that we have MacPorts on our side. The following commands will install cheat on your system:
sudo port install ruby rb-rubygems sudo gem install cheat --include-dependencies
To view a list of cheat sheets, run this command:
cheat sheets
Then to view a cheat sheet, just enter “cheat” followed by whatever sheet you wish to view. So to view the cheat sheet about tar, the popular file archiving utility, you just run this command:
cheat tar
It’s worth noting that most of Cheat’s sheets are about Ruby-related topics, since it is a tool written in Ruby, by members of the Ruby community. However, anybody is free to add new cheat sheets, and the list is always expanding.
Another way to enable colors is to add this alias to .bash_login
alias ls='/bin/ls -FG'
-F adds a bit more info to the output (so you know when you're dealing with a file, a directory, a soft-link, etc.).
-G adds color to the output.
I think both options are essential to a pleasant Terminal experience.
See man ls for more info.
Thanks for the great post!
wow... cheat sheets... didn't even know they existed. how cool. You should do some more on this series for all us ignorant mac users out here.
Using Panther, I had to use pico to edit text in the terminal. Also had to edit ".profile" instead of ".bash_login".
Bash is Bash right? But programs like nano only come with certain installations of OS X...
But your article was worth it just to color code ls! =)
Most items such as environmentals, etc should go in either .profile or .bash_profile. These are executed with each shell.
.bash_login is executed only when using a login shell, but a more advanced user who's going to be using the terminal will know that and put items in each accordingly.