跳转到主内容

路径

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

功能

显示当前路径。

示例配置

{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#61AFEF",
"options": {
"style": "folder",
"mapped_locations": {
"C:\\temp": ""
}
}
}

选项

NameTypeDefaultDescription
folder_separator_iconstring/the symbol to use as a separator between folders
folder_separator_templatestringthe template to use as a separator between folders
home_iconstring~the icon to display when at $HOME
folder_iconstring..the icon to use as a folder indication
windows_registry_iconstring\uF013the icon to display when in the Windows registry
styleenumagnosterhow to display the current path
mixed_thresholdnumber4the maximum length of a path segment that will be displayed when using Mixed
max_depthnumber1maximum path depth to display before shortening when using agnoster_short
max_widthany0maximum path length to display when using powerlevel or agnoster, can leverage templates
hide_root_locationbooleanfalsehides the root location if it doesn't fit in the last max_depth folders when using agnoster_short
cycle[]stringa list of color overrides to cycle through to colorize the individual path folders, e.g. [ "#ffffff,#111111" ]
cycle_folder_separatorbooleanfalsecolorize the folder_separator_icon as well when using a cycle
folder_formatstring%sformat to use on individual path folders
edge_formatstring%sformat to use on the first and last folder of the path
left_formatstring%sformat to use on the first folder of the path - defaults to edge_format
right_formatstring%sformat to use on the last folder of the path - defaults to edge_format
gitdir_formatstringformat to use for a git root directory
display_cygpathbooleanfalsedisplay the Cygwin style path using cygpath -u $PWD
display_rootbooleanfalsedisplay the root / on Unix systems
dir_lengthnumber1the length of the directory name to display when using fish
full_length_dirsnumber1indicates how many full length directory names should be displayed when using fish

映射位置

允许用图标/字符串覆盖特定路径位置。 系统会验证当前路径是否以特定元素开头,匹配成功则替换为对应图标/字符串。 为避免嵌套覆盖问题,Oh My Posh 会在执行替换前对映射位置列表进行排序。

NameTypeDefaultDescription
mapped_locations_enabledbooleantruereplace known locations in the path with the replacements before applying the style
mapped_locationsobjectcustom glyph/text for specific paths. Works regardless of the mapped_locations_enabled

设置。

例如,要将 C:\Users\Leet\GitHub 替换为 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\FooC:\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\developmentLetter 模式会显示为 C\d\d\d\development)。唯一性指代同级目录的区分性,因此 C:\projectA\devC:\projectB\dev 均会显示为 C\p\dev

Powerlevel(Powerlevel 模式)

类似 unique,但当达到 max_width 时停止缩短。

Fish(Fish 风格模式)

类似 letter 模式,但会显示每个文件夹名称的前 dir_length 个字符(最后 full_length_dirs 指定数量的文件夹除外,这些会显示全称)。灵感源自 Fish shell 的 PWD 设计。

模板 (信息)

默认模板
 {{ .Path }}

属性

NameTypeDescription
.Pathstringthe current directory (based on the style property)
.Parentstringthe 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)
.RootDirbooleantrue if we're at the root directory (no parent)
.Locationstringthe current directory (raw value)
.StackCountintthe stack count
.Writablebooleanis the current directory writable by the user or not
.Formatfunctionformat any path based on the segment's settings (e.g. {{ .Format .Segments.Git.RelativeDir }})