什么是 Fish shell ?
很多做前端开发的朋友都很钟爱使用Mac电脑,同大多数Linux系统一样Mac自带的命令行是Bash提供的,是一种很基本的Shell。今天我们来了解一款高级的shell工具 -- Fish shell
和Bash相同,Fish shell 也是一种可以在Mac和Linux等系统使用的Shell。
和Bash不同,Fish shell 具有很好的易用性,可配置性等特点。
配置好Fish shell之后,很多的鼠标和触控板的操作,都可以在命令终端中完成了。真正做到弹指如飞,挥洒自如。
Fish shell 有哪些优点 ?
-
开箱即用
大家可以从 Fish shell 官网 直接下载安装包,或者通过homebrew安装。安装好之后无需配置,就可以直接使用,畅享其诸多好处了。
brew install fish //homebrew安装fishshell的命令 -
自动提示
-
Fish shell 有很好的命令提示功能。 下图为合法命令可以自动提示补全,
下图为非法命令会展示为红色。 -
Fish shell 的命令历史记录也很强大。 使用过的命令,当再次输入的时候,可以自动提示补全上次使用的参数。
-
-
图形化的配置页面
运行 fish_config 命令,就可以唤起 fish 的配置页面。如下图:
我最常用的前三个标签页:其中color页用于配置颜色主题;prompt页用于配置命令行的前缀;function页展示已经配置的函数列表,这也是我认为fish shell最强大的地方。
强大的函数
Fish shell 的函数十分强大,几乎可以满足你的所有需求。
Fish shell 的函数本质上是一个可执行脚本。允许你通过命令行输入函数名来执行脚本中的命令。听起来很像其他shell中的alias, 但它远比alias功能丰富。
函数的创建方法十分简单,只需进入 Fish shell 的配置目录(~/.config/fish/)并创建config.fish文件。直接在其中编写function就可以了。在命令行直接输入函数名可调用函数。
函数语法可以参考阮一峰老师关于 Fish shell 的博文。
分享一个我写的一个很简单的函数。
function push
echo "+++++git pull ++++++++++++++"
git pull
echo "+++++git add .++++++++++++++"
git add .
echo "+++++git commit ++++++++++++"
if test -d $argv
git commit -m (date +%Y-%m-%d_%H:%M:%S)
else
git commit -m $argv
end
echo "+++++git push +++++++++++++++"
git push
echo "++++++Finish+++++++++++++++++"
end
push函数的功能是用一个push 命令完成向git上提交代码的一连串操作。值得注意的是,可以在push可以接收一个参数,作为commit时候的注释。如果不提供参数,则默认将当前时间作为commit的注释。调用方法如下:
# 添加参数,参数中不能有空格
$ push your_first_commit
# 不添加参数,以当前时间为commit注释
$ push
最后,下面贴出我的config.fish文件, 以供参考。其中定义了用的比较顺手的几个函数,以及更改了环境变量的设置。
# 快速进入常用工作目录
function cdp
cd /Users/gavin/Documents/project
end
# 快速提交代码,使用方法:
# 可以添加一个参数作为 git commit 的注释
# 如果不添加参数会将当前时间作为 git commit 的注释
function push
echo "+++++git pull ++++++++++++++"
git pull
echo "+++++git add .++++++++++++++"
git add .
echo "+++++git commit ++++++++++++"
if test -d $argv
git commit -m (date +%Y-%m-%d_%H:%M:%S)
else
git commit -m $argv
end
echo "+++++git push +++++++++++++++"
git push
echo "++++++Finish+++++++++++++++++"
end
# 给 npm start 做的别名
function ss
npm start
end
# 给 npm install 做的别名
function sd
npm install
end
# 用 Vscode 打开当前目录
function oc
open . -a /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron
end
# 用 sublime text 打开当前目录
function os
open . -a /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text
end
# 在Finder中打开当前目录
function oo
open .
end
# 添加环境变量
set PATH /Users/gavin/tools/flutter/bin $PATH
感谢您的时间,也欢迎大家的建议和意见,谢谢~