Repository navigation
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
59 lines (49 loc) · 2.04 KB
/
Copy pathMicrosoft.PowerShell_profile.ps1
File metadata and controls
59 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 在函数外部定义,只在 Profile 加载时检查一次
$script:hasGit = Get-Command git -ErrorAction SilentlyContinue
$script:currentUserHost = "$Env:USERNAME@$Env:COMPUTERNAME"
function prompt {
# 基础信息
$user = $Env:USERNAME
$computer = $Env:COMPUTERNAME
$location = (Get-Location).Path -split "\\"
# 路径截断逻辑
if ($location.Count -gt 5) {
$displayPath = "..\" + ($location[-5..-1] -join "\")
} else {
$displayPath = (Get-Location).Path
}
# 获取 Git 分支 (2>$null 确保不在 git 目录时不报错)
$gitBranch = ""
if ($script:hasGit -and (Test-Path .git -ErrorAction SilentlyContinue)) {
$branch = git branch --show-current 2>$null
if ($branch) { $gitBranch = " [$branch]" }
}
# 时间戳
$timestamp = Get-Date -Format "HH:mm:ss"
# 输出提示符
Write-Host -NoNewline -ForegroundColor Cyan $script:currentUserHost
Write-Host -NoNewline " in "
Write-Host -NoNewline -ForegroundColor Yellow $displayPath
# 时间标识
Write-Host -NoNewline " [$timestamp]"
# Git分支标识
if ($gitBranch) {
Write-Host -NoNewline -ForegroundColor Magenta $gitBranch
}
# 换行后返回输入提示符
"`n> "
}
# Import the Chocolatey Profile that contains the necessary code to enable
# tab-completions to function for `choco`.
# Be aware that if you are missing these lines from your profile, tab completion
# for `choco` will not function.
# See https://ch0.co/tab-completion for details.
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
# 2. 修改 Tab 键补全方式:按一次 Tab 显示菜单,而不是一个个切换
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
# 3. 设置智能历史搜索
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward