Developing on Staxmanade

Slightly modified “CD” Command for Powershell

Background

In my previous job, I spent all my development time in a Linux environment. Was rather impressed at how much could get done at the command line, and how efficient some of those tasks became. My next job was based on Windows and mostly the Microsoft stack of development tools. This  meant I pretty much left the command line behind. That was, until, I started using git. And since I wanted to learn PowerShell, I used PowerShell to execute my git commands.
One thing that has bugged me for a while is simply moving between directories. Even with tab completion, all that typing is a still quite annoying. Especially if you jump between a set of similar directories. One feature from the Linux CD command that I missed was “CD -". This command in Linux can be used to jump to the previous directory (and then back again). One limitation of this command is it only could jump back to the previous directory, and it did not retain a memory of recent directories. There may be something better in Linux that I don’t know of, but I’m basing this on a limited experience a number of years ago.
So I threw a question out on twitter.
image_thumb6
After several tweets back and forth, @cwprogram threw an interesting spike at me.
image_thumb9[4]  http://pastebin.com/xwtkn0am
Although this wasn’t exactly what I was looking for, it contained enough of what I needed to spark my curiosity to write a version of my own.
And so a little script was born that I’m now using to replace the “CD” command in my PowerShell runtime.

What does this do?

After you get it installed (see install steps below), when you type “CD” with no parameters at the command prompt. It will list up to 10 of the most recent distinct paths you’ve been to recently. This list also gives an index lookup number that you can use as a shortcut to jump to that path.
Example:
C:\code> cd
     1) C:\Users\jasonj
     2) D:\temp
C:\code> cd 2
D:\temp>
You can continue to use the “CD” command to do your usual changing directories. Now you can quickly get a history of where you’ve been, and quickly jump to any of those previous histories without typing the entire paths again.
It defaults to only showing you the last 10 distinct items, but if you find yourself needing to go back farther than that, you can use the following command to list more than 10 items.
D:\temp> cd -ShowCount 100

How to Install

  1. Download the file and save it to a location you can reference later.
    https://github.com/staxmanade/DevMachineSetup/blob/master/GlobalScripts/Change-Directory.ps1
  2. Open your $PROFILE (What is that?)
  3. Type the following two commands into your profile to replace the existing “CD” command with the new one.

    Remove-Item alias:cd
    Set-Alias cd {Your_Saved_Directory}\Change-Directory.ps1
  4. Exit your PowerShell console and start a new one up.

Happy Commanding!

Comments

Jason.Jarrett
Thanks for the tip.
Bartek Bielawski
If you want some more *nix features, including cd - you may want to look at pscx.codeplex.com - I made mistake of ignoring what it has to offer and also re-invented cd- in my module. ;) IMO pscx should be added to win build by default. ;)

Comments