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?