您可以更换$HOME
with ~
使用普通的字符串替换。前任。
获取当前提示函数:
Get-Content Function:\prompt
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
然后更换$home
with ~
当当前路径为$home
or $home\*
.
使用开关(可读):
function global:prompt {
$path = switch -Wildcard ($executionContext.SessionState.Path.CurrentLocation.Path) {
"$HOME" { "~" }
"$HOME\*" { $executionContext.SessionState.Path.CurrentLocation.Path.Replace($HOME, "~") }
default { $executionContext.SessionState.Path.CurrentLocation.Path }
}
"PS $path$('>' * ($nestedPromptLevel + 1)) ";
}
使用正则表达式(推荐):
function global:prompt {
$regex = [regex]::Escape($HOME) + "(\\.*)*$"
"PS $($executionContext.SessionState.Path.CurrentLocation.Path -replace $regex, '~$1')$('>' * ($nestedPromptLevel + 1)) ";
}
Using Split-Path
(ugly):
function global:prompt {
$path = $executionContext.SessionState.Path.CurrentLocation.Path
$p = $path
while ($p -ne "") {
if($p -eq $HOME) { $path = $path.Replace($HOME,"~"); break}
$p = Split-Path $p
}
"PS $path$('>' * ($nestedPromptLevel + 1)) ";
}
Demo:
PS C:\> cd ~
PS ~> cd .\Desktop
PS ~\Desktop>