路径
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
功能
显示当前路径。
示例配置
- json
- yaml
- toml
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#61AFEF",
"options": {
"style": "folder",
"mapped_locations": {
"C:\\temp": ""
}
}
}
type: path
style: powerline
powerline_symbol:
foreground: "#ffffff"
background: "#61AFEF"
options:
style: folder
mapped_locations:
C:\temp:
type = "path"
style = "powerline"
powerline_symbol = ""
foreground = "#ffffff"
background = "#61AFEF"
[options]
style = "folder"
[options.mapped_locations]
"C:\\temp" = ""
选项
| Name | Type | Default | Description |
|---|---|---|---|
folder_separator_icon | string | / | the symbol to use as a separator between folders |
folder_separator_template | string | the template to use as a separator between folders | |
home_icon | string | ~ | the icon to display when at $HOME |
folder_icon | string | .. | the icon to use as a folder indication |
windows_registry_icon | string | \uF013 | the icon to display when in the Windows registry |
style | enum | agnoster | how to display the current path |
mixed_threshold | number | 4 | the maximum length of a path segment that will be displayed when using Mixed |
max_depth | number | 1 | maximum path depth to display before shortening when using agnoster_short |
max_width | any | 0 | maximum path length to display when using powerlevel or agnoster, can leverage templates |
hide_root_location | boolean | false | hides the root location if it doesn't fit in the last max_depth folders when using agnoster_short |
cycle | []string | a list of color overrides to cycle through to colorize the individual path folders, e.g. [ "#ffffff,#111111" ] | |
cycle_folder_separator | boolean | false | colorize the folder_separator_icon as well when using a cycle |
folder_format | string | %s | format to use on individual path folders |
edge_format | string | %s | format to use on the first and last folder of the path |
left_format | string | %s | format to use on the first folder of the path - defaults to edge_format |
right_format | string | %s | format to use on the last folder of the path - defaults to edge_format |
gitdir_format | string | format to use for a git root directory | |
display_cygpath | boolean | false | display the Cygwin style path using cygpath -u $PWD |
display_root | boolean | false | display the root / on Unix systems |
dir_length | number | 1 | the length of the directory name to display when using fish |
full_length_dirs | number | 1 | indicates how many full length directory names should be displayed when using fish |
映射位置
允许用图标/字符串覆盖特定路径位置。 系统会验证当前路径是否以特定元素开头,匹配成功则替换为对应图标/字符串。 为避免嵌套覆盖问题,Oh My Posh 会在执行替换前对映射位置列表进行排序。
| Name | Type | Default | Description |
|---|---|---|---|
mapped_locations_enabled | boolean | true | replace known locations in the path with the replacements before applying the style |
mapped_locations | object | custom glyph/text for specific paths. Works regardless of the mapped_locations_enabled |
设置。
例如,要将 C:\Users\Leet\GitHub 替换为 GitHub 图标,可进行如下配置:
- json
- yaml
- toml
{
"mapped_locations": {
"C:\\Users\\Leet\\GitHub": ""
}
}
mapped_locations:
C:\Users\Leet\GitHub:
[mapped_locations]
"C:\\Users\\Leet\\GitHub" = ""
工作原理
-
为实现跨平台映射,请使用
/作为路径分隔符,Oh My Posh 会根据运行的操作系统自动匹配有效分隔符。 -
若需匹配所有子目录,可使用通配符
*,例如:"C:/Users/Bill/*": "$"会将C:/Users/Bill/Downloads转换为$/Downloads,而C:/Users/Bill保持不变。 -
映射位置开头的
~字符会匹配用户主目录。 -
匹配规则在 Windows 和 macOS 上不区分大小写,其他操作系统则区分大小写。这意味着: 对于用户 Bill(Windows 账户名为
Bill,Linux 账户名为bill),~/Foo在 Windows 可能匹配C:\Users\Bill\Foo或C:\Users\Bill\foo,而在 Linux 仅匹配/home/bill/Foo。
为防止路径元素显示异常,当替换值包含文本样式标签(如 <lightGreen>...</>)时,
应避免在 folder_separator_icon 属性中使用尖括号字符(</>),反之亦然。
使用正则表达式
复杂场景下可使用 re: 前缀配合捕获组进行正则匹配。
该功能基于 Golang 的 regexp 包,支持所有兼容语法。替换值将采用第一个捕获组内容,后续捕获组将被忽略。
例如,"re:(C:/[0-9]+/Foo)": "#" 会匹配 C:\123\Foo\Bar 并替换为 #\Bar。匹配时路径始终使用 / 分隔符,
确保跨平台兼容性。
与标准替换规则相同:在 Windows 和 WSL 挂载驱动器上不区分大小写,其他操作系统区分大小写。
样式
样式决定路径的显示方式。根据历史经验和流行主题,提供以下 10 种模式:
-
agnoster -
agnoster_full -
agnoster_short -
agnoster_left -
full -
folder -
mixed -
letter -
unique -
powerlevel -
fish
Agnoster
用 folder_icon 表示中间目录,以 folder_separator_icon 分隔。
首尾目录(当前目录)始终显示原始名称。
Agnoster Full
显示完整目录名,以 folder_separator_icon 分隔。
Agnoster Short(Agnoster 精简模式)
当路径深度超过 max_depth 层级时,会显示一个 folder_icon(若 hide_root_location 设为 false,则根目录不计入深度),后接最后 max_depth 个层级的文件夹名称,各名称间以 folder_separator_icon 分隔。
Agnoster Left(Agnoster 左显模式)
将每个文件夹显示为 folder_icon 并以 folder_separator_icon 分隔。仅完整显示首层文件夹及其下一级子目录。
Full(完整模式)
将当前工作目录以完整字符串显示,各文件夹间以 folder_separator_icon 分隔。
Folder(当前目录模式)
仅显示当前所在文件夹名称。
Mixed(混合模式)
类似 agnoster,但对长度较短的中间文件夹名称会完整显示。可显示文件夹名称的最大长度由 mixed_threshold 属性控制。
Letter(首字母模式)
类似 agnoster_full 模式,但会将每个文件夹名称缩写为首字母(以符号或图标开头的名称除外)。特别说明:最后一个(当前)文件夹始终显示全称。
-
folder将缩写为f -
.config将缩写为.c -
__pycache__将缩写为__p -
➼ folder将缩写为➼ f
Unique(唯一缩写模式)
类似 letter 模式,但确保每个文件夹名称采用最短唯一缩写。
唯一性针对显示路径生效。例如 C:\dev\dev\dev\development 将显示为 C\d\de\dev\development(Letter 模式会显示为 C\d\d\d\development)。唯一性不指代同级目录的区分性,因此 C:\projectA\dev 和 C:\projectB\dev 均会显示为 C\p\dev。
Powerlevel(Powerlevel 模式)
类似 unique,但当达到 max_width 时停止缩短。
Fish(Fish 风格模式)
类似 letter 模式,但会显示每个文件夹名称的前 dir_length 个字符(最后 full_length_dirs 指定数量的文件夹除外,这些会显示全称)。灵感源自 Fish shell 的 PWD 设计。
模板 (信息)
{{ .Path }}
属性
| Name | Type | Description |
|---|---|---|
.Path | string | the current directory (based on the style property) |
.Parent | string | the current directory's parent folder which ends with a path separator (designed for use with style folder, it is empty if .Path contains only one single element) |
.RootDir | boolean | true if we're at the root directory (no parent) |
.Location | string | the current directory (raw value) |
.StackCount | int | the stack count |
.Writable | boolean | is the current directory writable by the user or not |
.Format | function | format any path based on the segment's settings (e.g. {{ .Format .Segments.Git.RelativeDir }}) |