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.

Is it a good idea to use the following alias:

cd() {
    pushd $1;
}

in bash?

I think this would be very useful, since I can then use a series of popds instead of just a cd - once.

Is there any case where this might be a problem?

share|improve this question
1  
I know in zsh there's a setting to do just this. Not sure what it is though. Is it necessary to do a function instead of just alias cd=pushd? –  Falmarri Nov 23 '10 at 18:15
1  
I'd second the alias over the function. –  alesplin Nov 23 '10 at 18:17
1  
Why not alias a new name instead. How about pd? Are you the only user and administrator of your system? –  Kevin Cantu Nov 23 '10 at 18:23
1  
@Falmarri The zsh option is autopushd –  Michael Mrozek Nov 23 '10 at 19:05
    
Strictly speaking, that's a function, not an alias (but all the better). –  tripleee Jul 24 '13 at 17:49

2 Answers 2

up vote 23 down vote accepted

Personally, I have these in my bashrc and use them all the time:

pushd()
{
  if [ $# -eq 0 ]; then
    DIR="${HOME}"
  else
    DIR="$1"
  fi

  builtin pushd "${DIR}" > /dev/null
  echo -n "DIRSTACK: "
  dirs
}

pushd_builtin()
{
  builtin pushd > /dev/null
  echo -n "DIRSTACK: "
  dirs
}

popd()
{
  builtin popd > /dev/null
  echo -n "DIRSTACK: "
  dirs
}

alias cd='pushd'
alias back='popd'
alias flip='pushd_builtin'

You can then navigate around on the command-line a bit like a browser. cd changes the directory. back goes to the previous directory that you cded from. And flip will move between the current and previous directories without popping them from the directory stack. Overall, it works great.

The only real problem that I'm aware of is the fact that it's then a set of commands that I'm completely used to but don't exist on anyone else's machine. So, if I have to use someone else's machine, it can be a bit frustrating. If you're used to just using pushd and popd directly, you don't have that problem. And while if you just alias cd put not popd, you won't have the issue of back not existing, you'll still have the problem that cd doesn't do quite what you expect on other machines.

I would note, however, that your particular implementation of cd doesn't quite work like cd in that the normal cd by itself will go to your home directory, but yours doesn't. The version that I have here doesn't have that problem. Mine also appends DIRSTACK onto the front of the dirs print out, but that's more a matter of personal taste more than anything.

So, as I said, I use these aliases all the time and have no problem with them. It's just that it can be a bit frustrating to have to use another machine and then find them not there (which shouldn't be surprising, but they're one of those things that you use so often that you don't think about them, so having them not work like you're used to can still be surprising).

share|improve this answer
    
Excellent advice! Vi/Vimperator users can configure the <C-o> to popd for authentic experience. –  progo Mar 20 '11 at 17:38
    
@progo can you explain how flip will change directory with popping from the directory stack? –  Jonathan. Sep 3 '13 at 11:42

This isn't a direct answer to the question, but I fell in love with the directory history window in 4DOS. So much so that I wrote my own version for Linux (and Cygwin). I've never gotten around to making it an easy-to-install utility, but if you know your way around a Bash prompt, it shouldn't be that hard to get running. Your question inspired me to put it into a Git repo and upload it to GitHub: dirhistory.

Basically, it's a daemon that collects directory changes from all your shells, and a Cdk program that displays the history and lets you pick any directory to switch to (so you're not limited to a stack). I find it really useful, and have it bound to Ctrl-PageUp, just like 4DOS did. (I even patched PuTTY so it would send Ctrl-PageUp to Bash.)

share|improve this answer

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.