Forked from backerman/profile-snippet-sshargcomplete.ps1
Created
February 16, 2023 14:16
-
-
Save goja288/4f95cf5d6b84703a6d6d4574f9cab59c to your computer and use it in GitHub Desktop.
Enable tab completion for ssh hostnames in PowerShell
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
| using namespace System.Management.Automation | |
| Register-ArgumentCompleter -CommandName ssh,scp,sftp -Native -ScriptBlock { | |
| param($wordToComplete, $commandAst, $cursorPosition) | |
| $knownHosts = Get-Content ${Env:HOMEPATH}\.ssh\known_hosts ` | |
| | ForEach-Object { ([string]$_).Split(' ')[0] } ` | |
| | ForEach-Object { $_.Split(',') } ` | |
| | Sort-Object -Unique | |
| # For now just assume it's a hostname. | |
| $textToComplete = $wordToComplete | |
| $generateCompletionText = { | |
| param($x) | |
| $x | |
| } | |
| if ($wordToComplete -match "^(?<user>[-\w/\\]+)@(?<host>[-.\w]+)$") { | |
| $textToComplete = $Matches["host"] | |
| $generateCompletionText = { | |
| param($hostname) | |
| $Matches["user"] + "@" + $hostname | |
| } | |
| } | |
| $knownHosts ` | |
| | Where-Object { $_ -like "${textToComplete}*" } ` | |
| | ForEach-Object { [CompletionResult]::new((&$generateCompletionText($_)), $_, [CompletionResultType]::ParameterValue, $_) } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment