Enhance Your Profile

A little over a year ago, I switched from PC-based front end development, to Apple/Mac based development. I had been doing Mac and PC desktop support in my previous job, so I was used to OSX, the Mac OS. However, the switch also required me to upgrade the tools I use on a daily basis, including my source code editor and web server platform. It also (finally!) got me working with the Git version-control software on a daily basis, something I had been looking forward to (all the cool kids use Git. Also, no more lost code.).

The best thing (to me) about using Git is that it has me using a Bash shell again, which means I can tweak out my .profile. What’s that you might ask? Why, let me show you!

When you open the Mac Terminal, you get a pretty basic command prompt:

JoesMacbookPro:~/CurrentDirectory User$

I find this wasteful. I know this is my computer, and I know my username. The current directory is nice,  but what’s up with the dollar sign? Luckily, it’s easy to change. Just edit the file .profile (you may have to run this first):

defaults write com.apple.finder AppleShowAllFiles TRUE;killall Finder;

To update the prompt to the minimal, just add this line:

export PS1="\w :"

…and reload your profile by running . ~/.profile

Now the command prompt looks like:

~/Sites/jqverify :

That \w is the current directory, and there are many, many other things it can display.

Another great tweak is to improve the history tool – by default it shows 500 lines in a very basic format: the line number and the command. Here’s what mine looks like:

1737 Nov 11, 15:10: cd ~/sites/project
1738 Nov 11, 15:11: git checkout disbranch
1739 Nov 11, 15:12: git pull origin disbranch
1740 Nov 11, 15:12: git merge master
1741 Nov 11, 15:12: git add sites/
1742 Nov 11, 15:13: git commit
1743 Nov 11, 15:13: git push origin disbranch

I have line number, date, time, and de-duplicated commands. My buffer is also 10,000 lines long. The settings for this:

HISTSIZE=10000
HISTFILESIZE=10000000
HISTCONTROL=ignoredups
HISTTIMEFORMAT='%b %d, %H:%M: '

By using this with grep, I can easily find obscure commands that I really really don’t want to mis-type, such as:

git reset --hard origin/master

If you have several codebases you work in, it gets tedious to switch between them, so I create aliases:

alias sluggo="cd /Users/joeashe/Sites/sluggo;git status"

This puts me in the right directory no matter where I’m at, and then runs a status to let me know what branch that repo is currently using. I have twenty aliases like this.

Aliases are serious time savers.  Any time I find myself searching for a command I use frequently – remote connection strings, secure copy commands, apache restarts, just about anything – if I use it often, it gets a nickname and becomes an alias. In a team setting, creating a custom profile on a staging server can serve the same purpose – shorthand for deployment scripts, database imports/exports, and other things that could go horribly wrong with a typo. I mean, seriously, why type:

sudo sh /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

When you could just type restartapache? (I actually have this command with two aliases, since I can never remember if it’s apacherestart or restartapache)

In fact, I tinker with my .profile so often, I have the refresh command as an alias, so I don’t have to log out: alias refresh=". ~/.profile"

~Joe K

Leave a Reply

Your email address will not be published. Required fields are marked *