Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have set up an alias in my .bashrc as follows:

alias u='cd ..'

All is well in my world... until I type cd .. and cringe that I did not use my incredible new alias. In fact, with this particular thing, it's very ingrained. Hard to change my behavior - I need serious intervention.

So, I naturally tried to set up another alias to keep me from using cd ..:

alias 'cd ..'='echo "Use your alias!"'

But that apparently doesn't work. My thought is that this also might somehow conflict with the u alias, in some sort of infinite loop of aliasing.

Any ideas?

share|improve this question
    
OK, to note: I don't care if this uses aliases or not - My real goal is to stop myself from using cd .. somehow. Perhaps I should change the question title? –  kenny May 7 '11 at 19:47
    
I used to use cd.. (no spaces) all the time in DOS. Took me a while to break that habit in Linux back in the day. –  ultrasawblade May 8 '11 at 3:32

1 Answer 1

up vote 8 down vote accepted

Anything more complicated that supplying a few extra arguments to a command is too much for an alias and requires a function instead. Use builtin cd to call the original.

cd () {
  if [ "$*" = ".." ]; then
    echo 1>&2 'Use your alias instead!'
    return 2
  else
    builtin cd "$@"
  fi
}

If you're running bash ≥4.0, I question the utility of this particular alias. Put shopt -s autocd in your ~/.bashrc, and just type .. or any other directory name to switch to it.

share|improve this answer
    
I might have to use this to break my cd habit... although... at work I wouldn't have access to zsh autocd... oh choices... –  xenoterracide May 8 '11 at 0:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.