Quickly moving up a directory tree

I am lazy and I have been working recently on optimizing my PowerShell profile to become faster from the command line. My most recent target was a command I run often to move up a directory:

> cd ../

Now that may not look like a lot of characters but it gets worse when I want to jump 3 or 4 levels up:

> cd ../../../../

Annoying! To fix this I added this little snippet to my PowerShell profile:

for($i = 1; $i -le 5; $i++){
  $u =  "".PadLeft($i,"u")
  $unum =  "u$i"
  $d =  $u.Replace("u","../")
  Invoke-Expression "function $u { push-location $d }"
  Invoke-Expression "function $unum { push-location $d }"
}

This beautiful bit of code generates two sets of functions. One lets me move up a directory a given number of times by running a command like:

> u4

This will move up four directories. The second set of functions does the same thing but by typing u the number of times you want to traverse up:

> uuuu

Now I can hear you saying “Hey Matt, You claim to be all about being fast from the command line so why would you ever run the command uuuu when you can just run u4?”

That is an astute observation nameless reader! But sometimes I am in a mood where it is satisfying to press one key repeatedly. Ok?

  • http://aaronweiker.com Aaron

    I don’t like the ‘u’ character… how about the ‘<'… yes, I realize it's actually two keys you are pressing to do it, but it does have the added semi-intuitive that you are going backwards. And if you are the in mood for going back a lot you could just do '<——–' and you magically go up 9 levels.

    • https://www.google.com/accounts/o8/id?id=AItOawlrLeowWytSCscAcNv3ky4tdtP7AcgDAC8 Matthew

      You can really use any character. I find ‘u’ very logical since I think of it as moving up a directory

  • Ashkan

    Great work on Chutzpah.

    Any chance you will be adding TypeScript support soon?

    • https://www.google.com/accounts/o8/id?id=AItOawlrLeowWytSCscAcNv3ky4tdtP7AcgDAC8 Matthew

      Definitely, I have the feature pretty much code complete but need to do more testing. I plan to release a new version with TypeScript support this weekend.