Fish的官網宣傳語是 Finally, a command line shell for the 90s。 翻譯過來就是 Fish shell 是一個為90後準備的 shell。
有人說:“二逼青年用bash,普通青年用zsh,文藝青年用fish。”[4]
其次由于zsh 的速度實在是太慢,是以決定換用fish,fish速度快,智能提示強大。
本文的亮點在于三點:
1、Fish的入門使用
2、與bash相容性的方案
3、一個屬于自己的Fish主題
Fish配置請參考:https://github.com/iceqing/linux-template-setting/blob/master/fish/config.fish
下面分别介紹
一、fish 入門使用
1 、ubuntu 安裝fish
apt-get install software-properties-common
sudo apt-add-repository ppa:fish-shell/release-2
sudo apt-get update
sudo apt-get install fish
#切換到fish
echo /usr/bin/fish | sudo tee -a /etc/shells
sudo chsh -s /usr/bin/fish && fish
其他平台類似,可以根據官網說明來 [1]
fish的鮮明特征在于安裝時已經預設內建了很多需要的功能。
比如:
- 指令行文法高亮,錯誤會顯示紅色
- 智能提示
- 可以使用web網頁的進行終端配置
fish 有智能提示,一個指令一旦輸入過一次,會自動顯示上一次的全部指令,細心一點會發現會有一層灰色的字型表示上一次的指令,按
Ctrl+F
或者 右方向鍵
→
, 即可自動補全,如下圖:

2、 安裝autojump
git clone https://github.com/wting/autojump.git
cd autojump
./install.py
vim ~/.config/fish/config.fish
按照install.py 指令給出的提示來修改config.fish, 添加
if test -f /home/ice/.autojump/share/autojump/autojump.fish; . /home/ice/.autojump/share/autojump/autojump.fish; end
3、網頁配置fish
fish_config
可以直接跳出網頁版本配置fish的界面。
web版本可以設定主題, 推薦其中的"Tomorrow Night"主題顔色。
image.png
選擇想要的主題,然後點選set theme即可設定主題。
在指令裡按enter 即可退出web版本的界面。
在prompt裡面可以自己選擇fish終端的主題。
fish終端的主題
4、插件管理
https://github.com/oh-my-fish/oh-my-fish
omf install thefuck
雖然有fisher這個管理工具,但是目前還不穩定。
5、git 配置
我們隻需要配置alias 既可以滿足日常git指令的使用。對于終端主題樣式,我們可以自行進行配置。(後邊有介紹)
# git 相關的配置
alias g "git"
alias gst "git status"
alias grs "git reset --soft"
alias grh "git reset --hard"
alias gb "git branch"
alias gba "git branch -a"
alias gl "git pull"
二、相容bash腳本
由于fish 很多不相容bash的功能導緻了很多腳本無法運作,這一點是很多人吐槽fish的地方,我們需要一種方式來運作bash腳本。
比如
arc land --onto `git rev-parse --abbrev-ref HEAD`
這條指令在fish裡無法執行。
我們隻需要在前面添加一個bash -c 指令即可,如下所示。
bash -c "arc land --onto `git rev-parse --abbrev-ref HEAD`"
順手加個alias就更友善了,可以直接在指令行裡使用指令
arcl
。
alias arcl bash -c "arc land --onto `git rev-parse --abbrev-ref HEAD`"
對于腳本檔案,比如我将需要執行的指令或檔案放到repomerge.sh
在~/.config/fish/config.fish添加
alias up "bash -c /usr/bin/repomerge.sh"
然後就可以自由的使用up指令了
配置自己的終端
樣式顯示如下:
其中function fish_prompt 函數用于定義fish終端的顯示樣式。
我們隻需要寫一個fish_prompt函數即可。內建了git的分支名稱以及目前的變化。
顯示的樣式如下:
說明:
✔代表目前git項目是幹淨的。
%1 表示有一個檔案未追蹤
+1 表示一個檔案已暫存
# 終端顯示樣式的配置
function fish_prompt --description 'Write out the prompt'
if not set -q __fish_prompt_normal
set -g __fish_prompt_normal (set_color normal)
end
__fish_git_prompt >/dev/null 2>&1
<span class="hljs-keyword">if</span> git_is_repo
<span class="hljs-keyword">if</span> not <span class="hljs-built_in">set</span> -q __git_cb
<span class="hljs-built_in">set</span> __git_cb (set_color blue)<span class="hljs-string">" ("</span>(set_color brred)(git branch | grep \* | sed <span class="hljs-string">'s/* //'</span>) (set_color -o bryellow)(__fish_git_prompt_informative_status)(set_color blue)<span class="hljs-string">")"</span>
end
end
<span class="hljs-keyword">if</span> not <span class="hljs-built_in">set</span> -q __fish_prompt_cwd
<span class="hljs-built_in">set</span> -g __fish_prompt_cwd (set_color <span class="hljs-variable">$fish_color_cwd</span>)
end
<span class="hljs-built_in">printf</span> <span class="hljs-string">'%s%s%s%s '</span> <span class="hljs-string">"<span class="hljs-variable">$__fish_prompt_cwd</span>"</span> (prompt_pwd) <span class="hljs-string">"<span class="hljs-variable">$__fish_prompt_normal</span>"</span> <span class="hljs-variable">$__git_cb</span>
end
隐藏歡迎語
在confin.sh檔案裡添加如下函數即可
function fish_greeting
end
其他配置
alias l "ll"
alias dir "dde-file-manager . &"
alias docker "sudo docker"
alias apt "sudo apt"
參考:
- 官網
- fish 安裝 https://launchpad.net/~fish-shell/+archive/ubuntu/release-2
- Fish shell 入門教程 (阮一峰) http://www.ruanyifeng.com/blog/2017/05/fish_shell.html
- 宇宙第一shell——fish入門 https://www.jianshu.com/p/7ffd9d1af788
全部配置如下
# git配置
alias g "git"
alias gst "git status"
alias grs "git reset --soft"
alias grh "git reset --hard"
alias gb "git branch"
alias gba "git branch -a"
alias gl "git pull"
# 系統相關配置
alias l "ll"
alias dir "dde-file-manager . &"
alias docker "sudo docker"
alias apt "sudo apt"
# 終端顯示樣式的配置
function fish_prompt --description 'Write out the prompt'
if not set -q __fish_prompt_normal
set -g __fish_prompt_normal (set_color normal)
end
__fish_git_prompt ><span class="hljs-regexp">/dev/null</span> <span class="hljs-number">2</span>>&<span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> git_is_repo
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> set -q __git_cb
set __git_cb (set_color blue)<span class="hljs-string">" ("</span>(set_color brred)(git branch <span class="hljs-params">| grep \* |</span> sed <span class="hljs-string">'s/* //'</span>) (set_color -o bryellow)(__fish_git_prompt_informative_status)(set_color blue)<span class="hljs-string">")"</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> set -q __fish_prompt_cwd
set -g __fish_prompt_cwd (set_color $fish_color_cwd)
<span class="hljs-keyword">end</span>
printf <span class="hljs-string">'%s%s%s%s '</span> <span class="hljs-string">"$__fish_prompt_cwd"</span> (prompt_pwd) <span class="hljs-string">"$__fish_prompt_normal"</span> $__git_cb
end
# 終端提示語配置
function fish_greeting
end
# 判斷是否是git倉庫的工具函數
function git_is_repo --description 'Check if directory is a repository'
test -d .git
or command git rev-parse --git-dir >/dev/null ^/dev/null
end
如果你喜歡在前面加上使用者名稱可以使用
printf '%s %s%s%s%s ' "$USER" "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" $__git_cb
替換掉上面的
printf '%s%s%s%s ' "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" $__git_cb
本文采用署名-相同方式共享 4.0 國際 (CC BY-SA 4.0),轉載請注明出處。
系列文章
1. Fish Shell使用心得
2. Fish Shell 3.0 新功能
原文位址:https://www.jianshu.com/p/bf03bce60987