zsh 的 theme 很多花里胡哨的还挺好看,可以上 oh my zsh theme wiki 上面查看,但是奇怪的是每次输入命令之后还有点卡,不知道为什么,于是我配置了fish。fish的优点主要是终端可以配置的更花里胡哨,以及可以根据历史记录自动补全命令,这个比Ctrl + R 用的舒服的多,但缺点也不是没有,比如 fish 下面写脚本不是那么习惯,可能和 bash 有点区别。
配置
fish 主要通过修改文件~/.config/fish/config.fish 配置。作用类似于~/.bashrc,但是里面编写指令不太舒服,但是可以配置更花哨的终端。下面提供两个示例,复制到上述文件里面就可以用了。先上效果图:
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分支文字颜色名称 --- iftest$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) iftest -n "$branch" set_color $git_branch_color_name --bold printf'(%s)'"$branch" set_color normal printf' ' end
# -- 辅助函数定义 (仅在此成功分支内有效和定义) -- 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) whiletest$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
iftest$i -eq 1 set current_arrow_color_hex $start_color_hex elseiftest$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
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分支文字颜色名称 --- iftest$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) iftest -n "$branch" set_color $git_branch_color_name --bold printf'(%s)'"$branch" set_color normal printf' ' end
iftest$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类似的初始化配置的迁移比较麻烦?就这样吧,渐变色的箭头我还是比较喜欢的。