Last active
April 29, 2026 13:04
-
-
Save jdharmon/ceae0031553ea67bdb17023a5b1d6439 to your computer and use it in GitHub Desktop.
Quickly navigate into local repository subdirectories with tab completion for the repository names.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $env:REPOS_PATH='C:\Source\Repos' | |
| function Set-RepoLocation ($name) { | |
| Set-Location $(Join-Path $env:REPOS_PATH $name) | |
| } | |
| Set-Alias cdr Set-RepoLocation | |
| Register-ArgumentCompleter -CommandName @('Set-RepoLocation', 'cdr') -ParameterName 'name' -ScriptBlock { | |
| param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) | |
| # Determine if we are searching the root or a sub-directory | |
| $lastSlash = $wordToComplete.LastIndexOf('\') | |
| if ($lastSlash -ge 0) { | |
| $parentPath = $wordToComplete.Substring(0, $lastSlash) | |
| $leafPart = $wordToComplete.Substring($lastSlash + 1) | |
| $searchDir = Join-Path $env:REPOS_PATH $parentPath | |
| $prefix = "$parentPath\" | |
| } else { | |
| $parentPath = "" | |
| $leafPart = $wordToComplete | |
| $searchDir = $env:REPOS_PATH | |
| $prefix = "" | |
| } | |
| # Only attempt to list if the target directory actually exists | |
| if (Test-Path $searchDir) { | |
| Get-ChildItem -Path $searchDir -Directory | | |
| Where-Object { $_.Name -like "$leafPart*" } | | |
| ForEach-Object { "$prefix$($_.Name)" } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment