如何在Linux上安裝友好的交互式shell:Fish
Fish,友好的交互式 shell 的縮寫,它是一個適于裝備于類 Unix 系統(tǒng)的智能而用戶友好的 shell。Fish 有著很多重要的功能,比如自動建議、語法高亮、可搜索的歷史記錄(像在 bash 中 CTRL+r)、智能搜索功能、極好的 VGA 顏色支持、基于 web 的設置方式、完善的手冊頁和許多開箱即用的功能。盡管安裝并立即使用它吧。無需更多其他配置,你也不需要安裝任何額外的附加組件/插件!
在這篇教程中,我們討論如何在 Linux 中安裝和使用 fish shell。
安裝 Fish
盡管 fish 是一個非常用戶友好的并且功能豐富的 shell,但并沒有包括在大多數 Linux 發(fā)行版的默認倉庫中。它只能在少數 Linux 發(fā)行版中的官方倉庫中找到,如 Arch Linux,Gentoo,NixOS,和 Ubuntu 等。然而,安裝 fish 并不難。
在 Arch Linux 和它的衍生版上,運行以下命令來安裝它。
- sudo pacman -S fish
在 CentOS 7 上以 root 運行以下命令:
- cd /etc/yum.repos.d/
- wget https://download.opensuse.org/repositories/shells:fish:release:2/CentOS_7/shells:fish:release:2.repo
- yum install fish
在 CentOS 6 上以 root 運行以下命令:
- cd /etc/yum.repos.d/
- wget https://download.opensuse.org/repositories/shells:fish:release:2/CentOS_6/shells:fish:release:2.repo
- yum install fish
在 Debian 9 上以 root 運行以下命令:
- wget -nv https://download.opensuse.org/repositories/shells:fish:release:2/Debian_9.0/Release.key -O Release.key
- apt-key add - < Release.key
- echo 'deb http://download.opensuse.org/repositories/shells:/fish:/release:/2/Debian_9.0/ /' > /etc/apt/sources.list.d/fish.list
- apt-get update
- apt-get install fish
在 Debian 8 上以 root 運行以下命令:
- wget -nv https://download.opensuse.org/repositories/shells:fish:release:2/Debian_8.0/Release.key -O Release.key
- apt-key add - < Release.key
- echo 'deb http://download.opensuse.org/repositories/shells:/fish:/release:/2/Debian_8.0/ /' > /etc/apt/sources.list.d/fish.list
- apt-get update
- apt-get install fish
在 Fedora 26 上以 root 運行以下命令:
- dnf config-manager --add-repo https://download.opensuse.org/repositories/shells:fish:release:2/Fedora_26/shells:fish:release:2.repo
- dnf install fish
在 Fedora 25 上以 root 運行以下命令:
- dnf config-manager --add-repo https://download.opensuse.org/repositories/shells:fish:release:2/Fedora_25/shells:fish:release:2.repo
- dnf install fish
在 Fedora 24 上以 root 運行以下命令:
- dnf config-manager --add-repo https://download.opensuse.org/repositories/shells:fish:release:2/Fedora_24/shells:fish:release:2.repo
- dnf install fish
在 Fedora 23 上以 root 運行以下命令:
- dnf config-manager --add-repo https://download.opensuse.org/repositories/shells:fish:release:2/Fedora_23/shells:fish:release:2.repo
- dnf install fish
在 openSUSE 上以 root 運行以下命令:
- zypper install fish
在 RHEL 7 上以 root 運行以下命令:
- cd /etc/yum.repos.d/
- wget https://download.opensuse.org/repositories/shells:fish:release:2/RHEL_7/shells:fish:release:2.repo
- yum install fish
在 RHEL-6 上以 root 運行以下命令:
- cd /etc/yum.repos.d/
- wget https://download.opensuse.org/repositories/shells:fish:release:2/RedHat_RHEL-6/shells:fish:release:2.repo
- yum install fish
在 Ubuntu 和它的衍生版上:
- sudo apt-get update
- sudo apt-get install fish
就這樣了。是時候探索 fish shell 了。
用法
要從你默認的 shell 切換到 fish,請執(zhí)行以下操作:
- $ fish
- Welcome to fish, the friendly interactive shell
你可以在 ~/.config/fish/config.fish 上找到默認的 fish 配置(類似于 .bashrc)。如果它不存在,就創(chuàng)建它吧。
自動建議
當我輸入一個命令,它以淺灰色自動建議一個命令。所以,我需要輸入一個 Linux 命令的前幾個字母,然后按下 tab 鍵來完成這個命令。
如果有更多的可能性,它將會列出它們。你可以使用上/下箭頭鍵從列表中選擇列出的命令。在選擇你想運行的命令后,只需按下右箭頭鍵,然后按下 ENTER 運行它。
無需 CTRL+r 了!正如你已知道的,我們通過按 CTRL+r 來反向搜索 Bash shell 中的歷史命令。但在 fish shell 中是沒有必要的。由于它有自動建議功能,只需輸入命令的前幾個字母,然后從歷史記錄中選擇已經執(zhí)行的命令。很酷,是吧。
智能搜索
我們也可以使用智能搜索來查找一個特定的命令、文件或者目錄。例如,我輸入一個命令的一部分,然后按向下箭頭鍵進行智能搜索,再次輸入一個字母來從列表中選擇所需的命令。
語法高亮
當你輸入一個命令時,你將注意到語法高亮。請看下面當我在 Bash shell 和 fish shell 中輸入相同的命令時截圖的區(qū)別。
Bash:
Fish:
正如你所看到的,sudo 在 fish shell 中已經被高亮顯示。此外,默認情況下它將以紅色顯示無效命令。
基于 web 的配置方式
這是 fish shell 另一個很酷的功能。我們可以設置我們的顏色、更改 fish 提示符,并從網頁上查看所有功能、變量、歷史記錄、鍵綁定。
啟動 web 配置接口,只需輸入:
- fish_config
手冊頁補完
Bash 和 其它 shells 支持可編程的補完,但只有 fish 可以通過解析已安裝的手冊來自動生成它們。
為此,請運行:
- fish_update_completions
實例輸出將是:
- Parsing man pages and writing completions to /home/sk/.local/share/fish/generated_completions/
- 3435 / 3435 : zramctl.8.gz
禁用問候語
默認情況下,fish 在啟動時問候你(“Welcome to fish, the friendly interactive shell”)。如果你不想要這個問候消息,可以禁用它。為此,編輯 fish 配置文件:
- vi ~/.config/fish/config.fish
添加以下行:
- set -g -x fish_greeting ''
你也可以設置任意自定義的問候語,而不是禁用 fish 問候。
- set -g -x fish_greeting 'Welcome to OSTechNix'
獲得幫助
這是另一個吸引我的令人印象深刻的功能。要在終端的默認 web 瀏覽器中打開 fish 文檔頁面,只需輸入:
- help
官方文檔將會在你的默認瀏覽器中打開。另外,你可以使用手冊頁來顯示任何命令的幫助部分。
- man fish
設置 fish 為默認 shell
非常喜歡它?太好了!設置它作為默認 shell 吧。為此,請使用命令 chsh:
- chsh -s /usr/bin/fish
在這里,/usr/bin/fish 是 fish shell 的路徑。如果你不知道正確的路徑,以下命令將會幫助你:
- which fish
注銷并且重新登錄以使用新的默認 shell。
請記住,為 Bash 編寫的許多 shell 腳本可能不完全兼容 fish。
要切換回 Bash,只需運行:
- bash
如果你想 Bash 作為你的永久默認 shell,運行:
- chsh -s /bin/bash
各位,這就是全部了。在這個階段,你可能會得到一個有關 fish shell 使用的基本概念。 如果你正在尋找一個Bash的替代品,fish 可能是一個不錯的選擇。