Using pushd and popd with tcsh


Aside from FreeBSD induced inertia, the other reason why I stick with tcsh as my primary *nix shell are the glorious pushd and popd stack commands.

I love stacks, they’re one of my all time favourite computer science concepts. They also remind me of hot pancakes with syrup even if they were really named for plates in diners. I’m mad about pancakes and I’m mad about stacks, sometimes even using the latter in situations where they’re not necessary or advised because they’re fun. You probably didn’t need or want to know any of this. Pancakes.

My point, and I do have one

The pushd and popd commands simply allow you to push and pop directories onto a stack. If you’re as into stacks as me you can probably already start to think of all the cool things you can do with this in a shell setting and in scripts.

In a practical example, I use pushd as a drop in replacement for the venerable cd command. I still enter cd, but I have pushd aliased to it in my .tcshrc file.

With it used in this way, each time I change to a different directory it gets added to the stack. This means, if if need to switch between two folders that would be tedious to rewrite each time, I can simply pop each off the stack using popd and appear back where I was. It also has a huge history, so you can go back to a ton of different folders.

It doesn’t sound like much, but it’s probably done more to improve both my sanity and productivity than anything else in a long while. Its such a more elegant solution than using the up arrow cursor to search my command history for the cd command I used before.

Some useful .[t]cshrc arguments

If you intend to use pushd and popd in this way, I’ve found these settings in my .tcshrc (or .cshrc if used with tcsh) make them more useful:

## Use pushd as a drop in cd replacement
alias cd 'pushd'
## Return to ~/ when given no arguments, like cd
set pushdtohome
## Don't print each directory added to stack
## I have the working dir shown in my prompt
set pushdsilent

Author bio and support


Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.