Powershell functions to navigate faster in the terminal

I’d like to share 3 Powershell functions that I’ve wrote this week to navigate a bit faster in the Powershell.

I’m having a lot of different folders to organize my files and projects and it’s kinda exhausting to tab through the folders with the command, which looks something like this:

cd D:\scTAB\dotnTAB\proTAB\paTAB\is\getTAB\long

What would be cool instead is

ccd d: sc dotn pro pa i ge lo

since I know the folder structure of paths I often visit.

(I did use the name ccd because the finger is on the c anyway when you want to type c)

With that goal in mind I wrote three functions which will achieve this beheaviour. The source can be found on Github.

Function gdp

The function gdp returns the path matching the abbreviations you give it as an argument.

If the folder is found, the output will be the following:

C:\Users\Markus> gdp ~ doc windo fun
~\Documents\WindowsPowerShell\Functions

It is possible to use special characters for home (~), root (\ or /) and another partition, e.g. d:.

If the argument is ambiguous, you will get a list of matching folders to sharpen your argument:

C:\Users\Markus> gdp d: s me
scm
software
's' is ambiguous.
In C:\Users\Markus\Documents\WindowsPowerShell\Functions\ccd.ps1:36 Zeichen:17
+                 throw "'$folder' is ambiguous."
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: ('s' is ambiguous.:String) [], RuntimeException
    + FullyQualifiedErrorId : 's' is ambiguous.

If the path is not found, it will just throw an error.

Function ccd

The function ccd is just a wraper around the gdp function, which means if the gdp function returns a valid path, it will change to the directory.

Function gfp

I liked the idea to use it with folders so I wrote another functions which does the same thing for files.

The function is called gfp (get file path) and it works the same way:

C:\Users\Markus> gfp ~ vimf vim
~\vimfiles\vimrc

I can also use it to give the path as a argument to another program or script:

C:\Users\Markus> gfp (~ vimf vim)

What happens is that vim will start and open the file „~\vimfiles\vimrc“.

This function also behaves like the gdp function in that it will list ambiguous filenames and throw an error if no file is found.

I hope you enjoy it!

comments powered by Disqus