Fish Shell
# Arch Linux / Garudasudo pacman -S fish
# Ubuntu / Debiansudo apt install fish
# macOSbrew install fish
# 或使用官网脚本curl -L https://get.oh-my.fish | fish设置为默认 Shell
Section titled “设置为默认 Shell”# 查看当前 shellecho $SHELL
# 查看已安装 shellscat /etc/shells
# 切换到 fishchsh -s /usr/bin/fish
# 或者直接运行fish🎯 智能自动补全
Section titled “🎯 智能自动补全”Fish 会自动建议历史命令和文件路径,按 → 或 Tab 接受建议。
# 输入 git 后按 Tab,会显示所有 git 子命令git <Tab># add branch checkout clone commit push pull ...
# 输入部分命令后显示灰色建议git che<Tab> # → git checkout🎨 语法高亮
Section titled “🎨 语法高亮”- ✅ 绿色 = 有效命令
- ❌ 红色 = 无效命令
- 🔗 蓝色 = 链接/路径
- 📁 下划线 = 目录
📜 历史搜索
Section titled “📜 历史搜索”# 输入部分命令后按 ↑,搜索历史ssh <Up> # 显示之前用过的 ssh 命令
# Ctrl+R 模糊搜索历史配置文件位置
Section titled “配置文件位置”~/.config/fish/├── config.fish # 主配置文件├── fish_variables # 变量存储├── completions/ # 自定义补全├── conf.d/ # 配置片段└── functions/ # 自定义函数Web 配置界面
Section titled “Web 配置界面”# 启动浏览器配置界面fish_config会打开浏览器,可以:
- 选择主题颜色
- 配置提示符
- 管理变量和函数
- 绑定快捷键
config.fish 示例
Section titled “config.fish 示例”# 环境变量set -gx EDITOR nvimset -gx VISUAL code
# PATHfish_add_path ~/binfish_add_path ~/.local/bin
# 别名alias g gitalias ll "ls -la"alias .. "cd .."alias ... "cd ../.."
# 启动问候语function fish_greeting echo "🐟 欢迎使用 Fish Shell!" echo "当前时间: "(date "+%Y-%m-%d %H:%M")endfunction mkcd mkdir -p $argv[1] cd $argv[1]end使用:mkcd new-project
与 Bash 的区别
Section titled “与 Bash 的区别”| 特性 | Bash | Fish |
|---|---|---|
| 赋值 | VAR=value | set VAR value |
| 导出 | export VAR=value | set -x VAR value |
| 数组 | ${array[@]} | $array |
| 命令替换 | $(cmd) 或 `cmd` | (cmd) |
| 条件 | [ $a -eq $b ] | test $a -eq $b |
| 字符串包含 | [[ $s == *pattern* ]] | string match -q "*pattern*" $s |
# 设置变量set myvar "hello"
# 全局环境变量set -gx MY_VAR "value"
# 追加到 PATHfish_add_path /new/path
# 数组操作set mylist a b cecho $mylist[1] # aecho $mylist[-1] # c (最后一个)
# 字符串操作set str "hello world"string length $str # 11string sub -l 5 $str # hellostring split " " $str # hello world (数组)
# 条件判断if test -f file.txt echo "文件存在"else if test -d dir echo "目录存在"else echo "都不存在"end
# 循环for file in *.txt echo $fileend
# 函数function hello echo "Hello, $argv[1]!"endhello "Fish" # Hello, Fish!Fisher(推荐)
Section titled “Fisher(推荐)”# 安装 Fishercurl -sL https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish | source && fisher install jorgebucaran/fisher
# 安装插件fisher install jorgebucaran/nvm.fish # Node 版本管理fisher install patrickf1/fzf.fish # fzf 集成fisher install jethrokuan/z # z 目录跳转Oh My Fish
Section titled “Oh My Fish”# 安装curl -L https://get.oh-my.fish | fish
# 主题omf install bobthefishgit clone https://github.com/oh-my-fish/theme-bobthefishomf theme bobthefish# 安装 Starship(跨 Shell 提示符)curl -sS https://starship.rs/install.sh | sh
# 添加到 config.fishecho 'starship init fish | source' >> ~/.config/fish/config.fishFish 不兼容 POSIX 脚本?
Section titled “Fish 不兼容 POSIX 脚本?”Fish 是交互式 Shell,不是 POSIX shell。如果需要运行 POSIX 脚本:
# 方法 1: 显式使用 bashbash script.sh
# 方法 2: shebang#!/bin/bash# 在脚本第一行指定解释器# 在 config.fish 中set fish_greeting