【花里胡哨】更好看的终端配置 - fish

spiritTrance Lv3

前言

zsh 的 theme 很多花里胡哨的还挺好看,可以上 oh my zsh theme wiki 上面查看,但是奇怪的是每次输入命令之后还有点卡,不知道为什么,于是我配置了fish。fish的优点主要是终端可以配置的更花里胡哨,以及可以根据历史记录自动补全命令,这个比Ctrl + R 用的舒服的多,但缺点也不是没有,比如 fish 下面写脚本不是那么习惯,可能和 bash 有点区别。

配置

fish 主要通过修改文件~/.config/fish/config.fish 配置。作用类似于~/.bashrc,但是里面编写指令不太舒服,但是可以配置更花哨的终端。下面提供两个示例,复制到上述文件里面就可以用了。先上效果图:

效果大致是,提示符号有七个箭头,会呈现出渐变的效果。如果上一条指令返回值是 -1,则会全红。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
if status is-interactive
# i8 # Commands to run in interactive sessions can go here
end
# ~/.config/fish/config.fish
# ~/.config/fish/functions/fish_prompt.fish
function fish_prompt
set -l last_status $status

# --- 在 if/else 块之前声明颜色变量,使其作用域为整个函数 ---
set -l user_host_bg_color
set -l path_bg_color
set -l git_branch_color_name

# --- 根据上一条命令的执行状态定义背景颜色和Git分支文字颜色名称 ---
if test $last_status -ne 0
# 命令失败时的颜色
set user_host_bg_color "red"
set path_bg_color "red"
set git_branch_color_name "red"
else
# 命令成功时的颜色
set user_host_bg_color "blue"
set path_bg_color "green"
set git_branch_color_name "yellow"
end

# --- 第一部分:信息行 (这部分保持不变) ---
set_color white --bold --background=$user_host_bg_color
printf '%s@%s' (whoami) (prompt_hostname)
set_color normal
printf ' '

set_color white --bold --background=$path_bg_color
printf '%s' $PWD
set_color normal
printf ' '

set -l branch (command git symbolic-ref --short HEAD 2>/dev/null; or command git rev-parse --short HEAD 2>/dev/null)
if test -n "$branch"
set_color $git_branch_color_name --bold
printf '(%s)' "$branch"
set_color normal
printf ' '
end

# --- 第二部分:7个箭头的提示符 ---
printf ' \n' # 行首空格和换行,让箭头在新一行开始

if test $last_status -eq 0
# 命令成功: 7个箭头,首尾随机颜色,中间插值

# -- 辅助函数定义 (仅在此成功分支内有效和定义) --
function __fish_prompt_hex_to_dec_comp
printf '%d' "0x$argv[1]"
end
function __fish_prompt_dec_to_hex_comp
printf '%02X' "$argv[1]"
end
# -- 辅助函数定义结束 --

set -l palette FF0000 FFA500 FFFF00 00FF00 0066FF 4B0082 EE82EE
set -l num_palette_colors (count $palette)
set -l num_arrows 7

set -l index1 (random 1 $num_palette_colors)
set -l index2 (random 1 $num_palette_colors)
while test $index1 -eq $index2
set index2 (random 1 $num_palette_colors)
end

set -l start_color_hex $palette[$index1]
set -l end_color_hex $palette[$index2]

set -l r_start (__fish_prompt_hex_to_dec_comp (string sub -s 1 -l 2 $start_color_hex))
set -l g_start (__fish_prompt_hex_to_dec_comp (string sub -s 3 -l 2 $start_color_hex))
set -l b_start (__fish_prompt_hex_to_dec_comp (string sub -s 5 -l 2 $start_color_hex))

set -l r_end (__fish_prompt_hex_to_dec_comp (string sub -s 1 -l 2 $end_color_hex))
set -l g_end (__fish_prompt_hex_to_dec_comp (string sub -s 3 -l 2 $end_color_hex))
set -l b_end (__fish_prompt_hex_to_dec_comp (string sub -s 5 -l 2 $end_color_hex))

# *** CORRECTION IS HERE ***
for i in (seq 1 $num_arrows) # 使用 (seq 1 $num_arrows) 代替 from ... to ...
set -l current_arrow_color_hex

if test $i -eq 1
set current_arrow_color_hex $start_color_hex
else if test $i -eq $num_arrows
set current_arrow_color_hex $end_color_hex
else
set -l t (math --scale=10 "($i - 1) / ($num_arrows - 1)")
set -l r_interp (math "round((1 - $t) * $r_start + $t * $r_end)")
set -l g_interp (math "round((1 - $t) * $g_start + $t * $g_end)")
set -l b_interp (math "round((1 - $t) * $b_start + $t * $b_end)")
set r_curr (math "max(0, min(255, $r_interp))")
set g_curr (math "max(0, min(255, $g_interp))")
set b_curr (math "max(0, min(255, $b_interp))")
set current_arrow_color_hex (__fish_prompt_dec_to_hex_comp $r_curr)(__fish_prompt_dec_to_hex_comp $g_curr)(__fish_prompt_dec_to_hex_comp $b_curr)
end
printf '%s➤ ' (set_color $current_arrow_color_hex)
end
set_color normal

else
# 命令失败: 7个红色箭头 (这里的循环语法 "for i in (seq 7)" 是正确的)
set_color red --bold
for i in (seq 7) # (seq 7) 会生成 1 2 3 4 5 6 7
printf '➤ '
end
set_color normal
end
printf ' '
end

然后下面这个则是七个箭头呈现彩虹色的效果,比较一般,图标就不展示了,就放在这里吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
if status is-interactive
i8 # Commands to run in interactive sessions can go here
end
# ~/.config/fish/config.fish

# ~/.config/fish/functions/fish_prompt.fish
function fish_prompt
set -l last_status $status

# --- 在 if/else 块之前声明颜色变量,使其作用域为整个函数 ---
set -l user_host_bg_color
set -l path_bg_color
set -l git_branch_color_name

# --- 根据上一条命令的执行状态定义背景颜色和Git分支文字颜色名称 ---
if test $last_status -ne 0
# 命令失败时的颜色
set user_host_bg_color "red"
set path_bg_color "red"
set git_branch_color_name "red"
else
# 命令成功时的颜色
set user_host_bg_color "blue"
set path_bg_color "green"
set git_branch_color_name "yellow"
end

# --- 第一部分:信息行 ---

# 1. 用户名@主机名
set_color white --bold --background=$user_host_bg_color
printf '%s@%s' (whoami) (prompt_hostname)
set_color normal
printf ' '

# 2. 完整的绝对路径
set_color white --bold --background=$path_bg_color
printf '%s' $PWD
set_color normal
printf ' '

# 3. Git 分支信息
set -l branch (command git symbolic-ref --short HEAD 2>/dev/null; or command git rev-parse --short HEAD 2>/dev/null)
if test -n "$branch"
set_color $git_branch_color_name --bold
printf '(%s)' "$branch"
set_color normal
printf ' '
end

# --- 第二部分:7个箭头的提示符 ---
printf ' \n' # 行首空格和换行,让箭头在新一行开始

if test $last_status -eq 0
# 命令成功: 7个彩虹色箭头
set -l rainbow_colors FF0000 FFA500 FFFF00 00FF00 0066FF 4B0082 EE82EE # 红 橙 黄 绿 蓝 靛 紫
for color_hex in $rainbow_colors
printf '%s➤ ' (set_color $color_hex)
end
set_color normal
else
# 命令失败: 7个红色箭头
set_color red --bold
for i in (seq 7)
printf '➤ '
end
set_color normal
end
end

后记

因为作者有一堆需要加载的环境变量放在~/.bashrc里面的,但是迁移到~/.config/fish/config.fish会比较麻烦,fish的脚本有一套固定的语法,所以作者是这样解决的:在~/.bashrc的最后一行启动fish,这样环境变量也就无缝继承过去了。然后试了一下,在 fish 里面执行脚本应该也是没有问题的有问题,似乎就bashrc类似的初始化配置的迁移比较麻烦?就这样吧,渐变色的箭头我还是比较喜欢的。

比如没有 unset指令,可以在前文提到的文件最后面加上alias unset 'set -e'解决。应该主要是一些内建指令可能有区别,不知道脚本语法有没有区别,应该是有的。比如上面的函数,if的结尾是end而不是fi.

  • Title: 【花里胡哨】更好看的终端配置 - fish
  • Author: spiritTrance
  • Created at : 2025-05-29 00:00:01
  • Updated at : 2025-05-29 00:48:44
  • Link: https://spirittrance.github.io/2025/05/29/misc_更好看的终端配置/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
【花里胡哨】更好看的终端配置 - fish