This is a modification of Edd Edmondson’s tip from his Mac OS X for Astrophysicists. (If you use X11.app and you want it to stop opening xterm check out that page.)
Add the following to your ~/.profile or ~/.bash_profile or whatever. If you don’t have one create ~/.profile by:
nano -w ~/.profile
and then paste this code into it:
if [ -e /tmp/.X11-unix/* ]
then
X11_FOLDER=/tmp/.X11-unix
currentUser=`(set `whoami`; echo $1)`
bb=`ls -l $X11_FOLDER | grep $currentUser`
bbb=${bb/*X/:}
usedDISPLAY=$bbb.0
export DISPLAY=$usedDISPLAY
fi