简介

Nushell

Nushell(简称 Nu)是一个全新的 Shell,与传统 Shell 最大的区别在于它将一切视为结构化数据(表格、列表、记录),而不是纯文本流。这意味着你可以像操作数据库一样操作命令行输出:

1ls | where size > 1mb | sort-by modified

Nu 同时支持 Windows / macOS / Linux,当前最新稳定版为 0.113.1

Starship

Starship 是一个用 Rust 编写的 Shell 提示符工具,速度极快(每次渲染约 1-5ms),使用单一的 TOML 配置文件,可以跨 Shell(Bash、Zsh、Fish、PowerShell、Nushell)共享同一套配置。当前最新版本为 1.25.1


安装 Nushell

方式一:winget(推荐 / Windows)

1winget install nushell

方式二:Scoop

1scoop install nu

方式三:cargo

1cargo install nu --locked

安装完成后,在终端输入 nu 即可进入 Nushell。

设置 Nushell 为默认 Shell

在 Windows Terminal 中,进入设置 → 配置文件 → 添加新配置文件,命令行指向 Nu 可执行文件路径:

C:\Users\<用户名>\AppData\Local\Programs\nu\bin\nu.exe

也可以在 PowerShell 中通过 $PROFILE 添加:

1nu

配置 Nushell

Nushell 的配置文件位于 %AppData%\nushell\config.nu,在 Nu 中可以用以下命令直接打开:

1config nu

以下是实际使用的配置(C:\Users\29016\AppData\Roaming\nushell\config.nu):

 1# Nushell Configuration
 2
 3use std
 4
 5# ─── Starship Prompt ───────────────────────────────────────────
 6$env.STARSHIP_SHELL = "nu"
 7$env.PROMPT_COMMAND = { ||
 8    starship prompt --cmd-duration $env.CMD_DURATION_MS $"--status=($env.LAST_EXIT_CODE)"
 9}
10$env.PROMPT_COMMAND_RIGHT = ""
11$env.PROMPT_INDICATOR = ""
12$env.PROMPT_INDICATOR_VI_INSERT = ": "
13$env.PROMPT_INDICATOR_VI_NORMAL = "〉"
14$env.PROMPT_MULTILINE_INDICATOR = "::: "
15
16# ─── Editor ───────────────────────────────────────────────────
17$env.EDITOR = "notepad"
18$env.VISUAL = "notepad"
19
20# ─── Aliases ──────────────────────────────────────────────────
21alias ll = ls -l
22alias la = ls -a
23alias lla = ls -la
24alias g = git
25alias c = clear
26alias vim = nvim
27alias python = python3
28
29# ─── PATH ─────────────────────────────────────────────────────
30$env.Path = ($env.Path | split row (char esep) | append 'C:\Users\29016\AppData\Local\Programs\nu\bin')
31
32# ─── Default Configurations ──────────────────────────────────
33$env.config.show_banner = false   # 隐藏启动 banner
34$env.config.history.max_size = 10000

配置说明

配置项 说明
$env.STARSHIP_SHELL = "nu" 告诉 Starship 当前 Shell 是 Nushell
$env.PROMPT_COMMAND 设置 Starship 为主提示符,传递持续时间和退出状态码
$env.PROMPT_INDICATOR 普通模式下提示符为空(由 Starship 接管)
$env.PROMPT_INDICATOR_VI_INSERT VI 插入模式提示符设为 :
$env.PROMPT_INDICATOR_VI_NORMAL VI 普通模式提示符
show_banner = false 隐藏 Nushell 启动时的 logo 横幅
history.max_size = 10000 限制历史记录最大条数

别名一览

别名 等价命令
ll ls -l
la ls -a
lla ls -la
g git
c clear
vim nvim
python python3

安装 Starship

方式一:winget(Windows)

1winget install starship

方式二:官方安装脚本

1curl -sS https://starship.rs/install.sh | sh

验证安装

1starship --version
2# starship 1.25.1

配置 Starship

Starship 的配置文件位于 ~\.config\starship.toml(即 C:\Users\<用户名>\.config\starship.toml)。

使用预设快速起步

Starship 内置了 12 个官方预设,推荐使用 Gruvbox Rainbow:

1starship preset gruvbox-rainbow -o ~\.config\starship.toml

其他热门预设:

预设 风格
gruvbox-rainbow 温暖大地色 + Powerline 箭头(推荐)
tokyo-night 深色蓝紫冷色调
pastel-powerline 粉彩 Powerline 风格
catppuccin-powerline Catppuccin 主题
pure-preset 极简单行提示符
no-runtime-versions 不显示运行时版本信息

自定义配置

以下是在 Gruvbox Rainbow 预设基础上实际使用的完整配置:

  1"$schema" = 'https://starship.rs/config-schema.json'
  2
  3scan_timeout = 500
  4
  5format = """
  6[](color_orange)\
  7$os\
  8$username\
  9[](bg:color_yellow fg:color_orange)\
 10$directory\
 11[](fg:color_yellow bg:color_aqua)\
 12$git_branch\
 13$git_status\
 14[](fg:color_aqua bg:color_blue)\
 15$c\
 16$cpp\
 17$rust\
 18$golang\
 19$nodejs\
 20$bun\
 21$php\
 22$java\
 23$kotlin\
 24$haskell\
 25$python\
 26[](fg:color_blue bg:color_bg3)\
 27$docker_context\
 28$conda\
 29$pixi\
 30[](fg:color_bg3 bg:color_bg1)\
 31$time\
 32[ ](fg:color_bg1)\
 33$line_break$character"""
 34
 35palette = 'gruvbox_dark'
 36
 37[palettes.gruvbox_dark]
 38color_fg0 = '#fbf1c7'
 39color_bg1 = '#3c3836'
 40color_bg3 = '#665c54'
 41color_blue = '#458588'
 42color_aqua = '#689d6a'
 43color_green = '#98971a'
 44color_orange = '#d65d0e'
 45color_purple = '#b16286'
 46color_red = '#cc241d'
 47color_yellow = '#d79921'
 48
 49[os]
 50disabled = false
 51style = "bg:color_orange fg:color_fg0"
 52
 53[os.symbols]
 54Windows = "󰍲"
 55Ubuntu = "󰕈"
 56# ... 其他系统图标
 57
 58[username]
 59show_always = true
 60style_user = "bg:color_orange fg:color_fg0"
 61style_root = "bg:color_orange fg:color_fg0"
 62format = '[ $user ]($style)'
 63
 64[directory]
 65style = "fg:color_fg0 bg:color_yellow"
 66format = "[ $path ]($style)"
 67truncation_length = 3
 68truncation_symbol = "…/"
 69
 70[directory.substitutions]
 71"Documents" = "󰈙 "
 72"Downloads" = " "
 73"Music" = "󰝚 "
 74"Pictures" = " "
 75"Developer" = "󰲋 "
 76
 77[git_branch]
 78symbol = ""
 79style = "bg:color_aqua"
 80format = '[[ $symbol $branch ](fg:color_fg0 bg:color_aqua)]($style)'
 81
 82[git_status]
 83style = "bg:color_aqua"
 84format = '[[($all_status$ahead_behind )](fg:color_fg0 bg:color_aqua)]($style)'
 85
 86# 语言模块(统一蓝底风格)
 87[nodejs]
 88symbol = ""
 89style = "bg:color_blue"
 90format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'
 91
 92[bun]
 93symbol = ""
 94style = "bg:color_blue"
 95format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'
 96
 97[c]
 98symbol = " "
 99style = "bg:color_blue"
100format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'
101
102[cpp]
103symbol = " "
104style = "bg:color_blue"
105format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'
106
107[rust]
108symbol = ""
109style = "bg:color_blue"
110format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'
111
112[golang]
113symbol = ""
114style = "bg:color_blue"
115format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'
116
117[python]
118symbol = ""
119style = "bg:color_blue"
120format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'
121
122[docker_context]
123symbol = ""
124style = "bg:color_bg3"
125format = '[[ $symbol( $context) ](fg:#83a598 bg:color_bg3)]($style)'
126
127[conda]
128style = "bg:color_bg3"
129format = '[[ $symbol( $environment) ](fg:#83a598 bg:color_bg3)]($style)'
130
131[pixi]
132style = "bg:color_bg3"
133format = '[[ $symbol( $version)( $environment) ](fg:color_fg0 bg:color_bg3)]($style)'
134
135[time]
136disabled = false
137time_format = "%R"
138style = "bg:color_bg1"
139format = '[[  $time ](fg:color_fg0 bg:color_bg1)]($style)'
140
141[line_break]
142disabled = false
143
144[character]
145disabled = false
146success_symbol = '[](bold fg:color_green)'
147error_symbol = '[](bold fg:color_red)'
148vimcmd_symbol = '[](bold fg:color_green)'
149vimcmd_replace_one_symbol = '[](bold fg:color_purple)'
150vimcmd_replace_symbol = '[](bold fg:color_purple)'
151vimcmd_visual_symbol = '[](bold fg:color_yellow)'

配置结构解析

整条提示符由 Powerline 风格的分段箭头连接,自左向右依次为:

模块 背景色 显示内容
$os 橙色 当前操作系统图标
$username 橙色 用户名
$directory 黄色 当前目录路径(截断至 3 级)
$git_branch 青色 Git 分支名
$git_status 青色 Git 状态(修改/暂存/冲突/ ahead/behind)
语言模块 蓝色 C/C++/Rust/Go/Node.js/Bun/PHP/Java/Kotlin/Haskell/Python 版本
$docker_context 灰 3 Docker 上下文
$conda 灰 3 Conda 环境
$pixi 灰 3 Pixi 环境
$time 灰 1 当前时间(24h)
$character 提示符箭头(绿色成功 / 红色失败)

检查配置

Starship 提供了方便的调试命令:

 1# 查看当前渲染的配置值
 2starship config
 3
 4# 查看各模块渲染耗时
 5starship timings
 6
 7# 查看预设默认配置
 8starship preset gruvbox-rainbow
 9
10# 检查 TOML 语法(需要 taplo)
11taplo lint ~\.config\starship.toml

Nushell + Starship 集成

将 Nushell 和 Starship 结合使用的关键配置在 config.nu 中:

1$env.STARSHIP_SHELL = "nu"
2$env.PROMPT_COMMAND = { ||
3    starship prompt --cmd-duration $env.CMD_DURATION_MS $"--status=($env.LAST_EXIT_CODE)"
4}

这段配置做了三件事:

  1. $env.STARSHIP_SHELL = "nu" — 设置环境变量,Starship 据此加载 Nushell 特有的提示符模块
  2. $env.PROMPT_COMMAND — 定义一个闭包,每次绘制提示符时调用 starship prompt,并传入命令执行时间 CMD_DURATION_MS 和上一条命令的退出状态码 LAST_EXIT_CODE
  3. 清空其它提示符变量PROMPT_COMMAND_RIGHTPROMPT_INDICATOR 等设为空,完全由 Starship 接管渲染

额外技巧

1. 目录图标替换

Starship 支持将常见目录名替换为 Nerd Font 图标,当前配置中的替换表:

原目录 替换图标
Documents 󰈙
Downloads
Music 󰝚
Pictures
Developer 󰲋

2. 提示符颜色随退出状态变化

Starship 的 $character 模块会根据上一条命令的退出码自动变色:成功为绿色 ,失败为红色 ,无需额外配置。

3. 调试慢提示符

如果感觉提示符响应变慢,使用 starship timings 查看各模块耗时。常见的耗时模块是 git_status,可以设置 scan_timeout = 500(当前已配置)来限制 Git 扫描超时。


参考