如何更改PowerShell提示符以仅显示父目录和当前目录? [英] How can I change the PowerShell prompt to show just the parent directory and current directory?
问题描述
我想缩短我的PowerShell提示符,使其仅显示父目录和当前目录。例如,如果密码是
C:\Users\ndunn\OneDrive\Documents\Webucator\ ClassFiles\python-basics\Demos
我希望提示符为:
PS ..\python-basics\Demos>
我可以将其设为 PS ..\Demos>
通过更改配置文件中的 prompt()
函数:
- 通过在PowerShell中运行
$ profile
查找配置文件的位置。 - 打开(或创建并打开)配置文件。 / li>
- 更改(或添加)以下
prompt()
函数:
功能提示
{
$ folder = $(((get-item $ pwd).Name)
PS ..\ $ folder>
}
我尝试使用 split()
和负索引,但无法使其正常工作。 / p>
此外,我只想在pwd至少降低两个级别的情况下执行此操作。如果pwd类似于C:\folder\folder,我想显示默认提示。
有什么想法吗?
尝试以下功能,该功能应在Windows和类似Unix的平台(在PowerShell Core 中)均适用:
函数global:prompt {
$ dirSep = [IO.Path] :: DirectorySeparatorChar
$ pathComponents = $ PWD.Path.Split ($ dirSep)
$ displayPath = if($ pathComponents.Count -le 3){
$ PWD.Path
} else {
'... {0} {1}' -f $ dirSep,($ pathComponents [-2,-1] -join $ dirSep)
}
" PS {0} $('>'*($ nestedPromptLevel + 1))" ; -f $ displayPath
}
请注意,我选择了单个字符…
(水平省略号, U + 2026
)表示路径的省略部分,因为 ..
可能与引用 parent 目录。
注意:非ASCII范围的…
字符只有在封闭脚本文件(假定为您的 $ PROFILE
文件-使用BOM表另存为UTF-8 [1] 或UTF-16LE( 。
如果由于某种原因对您不起作用,请使用三个不同的句点('...'
而不是'…'
),但请注意,这将导致提示时间更长。
[1] BOM仅在 Windows PowerShell 中是必需的;相比之下,PowerShell Core 默认情况下采用UTF-8,因此不需要BOM。
I would like to shorten my PowerShell prompt so that it just shows the parent directory and the current directory. For example, if the pwd is
C:\Users\ndunn\OneDrive\Documents\Webucator\ClassFiles\python-basics\Demos
I want the prompt to be:
PS ..\python-basics\Demos>
I can get it to be just PS ..\Demos>
by changing the prompt()
function in the Profile file:
- Find location of Profile file by running
$profile
in PowerShell. - Open (or create and open) Profile file.
- Change (or add) the following
prompt()
function:
function prompt
{
$folder = "$( ( get-item $pwd ).Name )"
"PS ..\$folder> "
}
I tried using split()
and negative indexing, but wasn't able to get it to work.
Also, I only want to do this if the pwd is at least two levels down. If the pwd is something like C:\folder\folder, I'd like to show the default prompt.
Any ideas?
Try the following function, which should work on Windows and Unix-like platforms (in PowerShell Core) alike:
function global:prompt {
$dirSep = [IO.Path]::DirectorySeparatorChar
$pathComponents = $PWD.Path.Split($dirSep)
$displayPath = if ($pathComponents.Count -le 3) {
$PWD.Path
} else {
'…{0}{1}' -f $dirSep, ($pathComponents[-2,-1] -join $dirSep)
}
"PS {0}$('>' * ($nestedPromptLevel + 1)) " -f $displayPath
}
Note that I've chosen single character …
(HORIZONTAL ELLIPSIS, U+2026
) to represent the omitted part of the path, because ..
could be confused with referring to the parent directory.
Note: The non-ASCII-range …
character is only properly recognized if the enclosing script file - assumed to be your $PROFILE
file - is either saved as UTF-8 with BOM[1] or as UTF-16LE ("Unicode").
If, for some reason, that doesn't work for you, use three distinct periods ('...'
instead of '…'
), though note that that will result in a longer prompt.
[1] The BOM is only a necessity in Windows PowerShell; by contrast, PowerShell Core assumes UTF-8 by default, so no BOM is needed.
这篇关于如何更改PowerShell提示符以仅显示父目录和当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!