profile #
默认配置脚本,会在启动PowerShell窗口时默认加载。
ps1
# 编写默认配置脚本
notepad $PROFILE
配置内容:
ps1
using namespace System.Management.Automation
## Prepare PSReadLine
#Import-Module PSReadLine
$PSReadLineOptions = @{
# Simulate Bash or Emacs
EditMode = "Emacs"
# PredictionViewStyle = "ListView"
HistoryNoDuplicates = $true
HistorySearchCursorMovesToEnd = $true
# AddToHistoryHandler = {
# Param([string]$line)
# if ($line -match "^git") {
# return $false
# } else {
# return $true
# }
# }
}
Set-PSReadLineOption @PSReadLineOptions
Set-PSReadlineKeyHandler -Chord Tab -Function MenuComplete
## Completion for ssh,scp,sftp
Register-ArgumentCompleter -CommandName ssh, scp, sftp -Native -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
function Get-SSHHostList($sshConfigPath) {
Get-Content -Path $sshConfigPath `
| Select-String -Pattern '^Host ' `
| ForEach-Object { $_ -replace 'Host ', '' } `
| ForEach-Object { $_ -split ' ' } `
| Sort-Object -Unique `
| Select-String -Pattern '^.*[*!?].*$' -NotMatch
}
function Get-SSHConfigFileList ($sshConfigFilePath) {
$sshConfigDir = Split-Path -Path $sshConfigFilePath -Resolve -Parent
$sshConfigFilePaths = @()
$sshConfigFilePaths += $sshConfigFilePath
Get-Content -Path $sshConfigFilePath `
| Select-String -Pattern '^Include ' `
| ForEach-Object { $_ -replace 'Include ', '' } `
| ForEach-Object { $_ -replace '~', $Env:USERPROFILE } `
| ForEach-Object { $_ -replace '\$Env:USERPROFILE', $Env:USERPROFILE } `
| ForEach-Object { $_ -replace '\$Env:HOMEPATH', $Env:USERPROFILE } `
| ForEach-Object {
$sshConfigFilePaths += $(Get-ChildItem -Path $sshConfigDir\$_ -File -ErrorAction SilentlyContinue -Force).FullName `
| ForEach-Object { Get-SSHConfigFileList $_ }
}
if (($sshConfigFilePaths.Length -eq 1) -and ($sshConfigFilePaths.item(0) -eq $sshConfigFilePath) ) {
return $sshConfigFilePath
}
return $sshConfigFilePaths | Sort-Object -Unique
}
$sshPath = "$Env:USERPROFILE\.ssh"
$hosts = Get-SSHConfigFileList "$sshPath\config" `
| ForEach-Object { Get-SSHHostList $_ } `
# 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
}
}
$hosts `
| Where-Object { $_ -like "${textToComplete}*" } `
| ForEach-Object { [CompletionResult]::new((&$generateCompletionText($_)), $_, [CompletionResultType]::ParameterValue, $_) }
}
## Launch gpg-agent
# gpg-connect-agent reloadagent /bye
# $env:SSH_AUTH_SOCK = "\\.\pipe\openssh-ssh-agent"