如何在Linux中安装Fish(友好的交互式Shell)

时间:2020-03-21 11:45:24  来源:igfitidea点击:

Fish是友好的交互式Shell的首字母缩写,是一种功能齐全,智能且用户友好的Shell,适用于类Unix系统。
Fish具有许多重要功能,例如自动建议,语法突出显示,可搜索的历史记录(例如Bash中的CTRL + r),智能搜索功能,出色的VGA颜色支持,基于Web的配置,手册页补全等,以及其他现成的功能。

只需安装并立即开始使用。
没有更多的另外配置,或者我们不必安装任何另外的加载项/插件!

在本教程中,让我们讨论如何在Linux中安装和使用fish shell。

在Linux上安装Fish

尽管fish是非常用户友好和功能丰富的shell,但大多数Linux发行版的默认存储库中均未包含fish。
它仅在少数Linux发行版的官方存储库中可用,例如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身份运行以下命令:

# echo 'deb http://download.opensuse.org/repositories/shells:/fish/Debian_9.0//' > /etc/apt/sources.list.d/shells:fish.list
# wget -nv https://download.opensuse.org/repositories/shells:fish/Debian_9.0/Release.key -O Release.key
# apt-key add - < Release.key
# apt-get update
# apt-get install fish

在Debian 8上以root身份运行以下命令:

# echo 'deb http://download.opensuse.org/repositories/shells:/fish/Debian_8.0//' > /etc/apt/sources.list.d/shells:fish.list
# wget -nv https://download.opensuse.org/repositories/shells:fish/Debian_8.0/Release.key -O Release.key
# apt-key add - < Release.key
# apt-get update
# apt-get install fish

在Fedora 29上,以root用户身份运行以下命令:

# dnf config-manager --add-repo https://download.opensuse.org/repositories/shells:fish/Fedora_29/shells:fish.repo
# dnf install fish

在Fedora 28上,以root用户身份运行以下命令:

# dnf config-manager --add-repo https://download.opensuse.org/repositories/shells:fish/Fedora_28/shells:fish.repo
# dnf 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

对于openSUSE Leap 42.3,以root用户身份运行以下命令:

# zypper addrepo https://download.opensuse.org/repositories/shells:fish/openSUSE_Leap_42.3/shells:fish.repo
# zypper refresh
# zypper install fish

对于openSUSE Leap 15.0,以root用户身份运行以下命令:

zypper addrepo https://download.opensuse.org/repositories/shells:fish/openSUSE_Leap_15.0/shells:fish.repo
zypper refresh
zypper install fish

在RHEL 7上以root用户身份运行以下命令:

# cd /etc/yum.repos.d/
# wget https://download.opensuse.org/repositories/shells:fish/RHEL_7/shells:fish.repo
# yum install fish

在RHEL 6上以root用户身份运行以下命令:

# cd /etc/yum.repos.d/
# wget https://download.opensuse.org/repositories/shells:fish/RHEL_6/shells:fish.repo
# yum install fish

在Ubuntu及其衍生版本上:

$sudo apt-get update
$sudo apt-get install fish

还有一个官方的PPA可用于Ubuntu及其衍生版本。
添加PPA并在Ubuntu上安装Fish,如下所示。

$sudo apt-add-repository ppa:fish-shell/release-3
$sudo apt-get update
$sudo apt-get install fish

用法

要从默认shell切换到fish,请执行以下操作:

$fish
Welcome to fish, the friendly interactive shell

我们可以在~/.config/fish/config.fish中找到默认的fish配置(类似于.bashrc)。
如果它不存在,只需创建它。

自动建议

当我键入命令时,它会自动建议一个浅灰色的命令。
因此,我必须输入Linux的前几个字母,然后按Tab键才能完成命令。

如果还有更多可能性,它将列出它们。
我们可以使用向上/向下箭头键从列表中选择任何命令。
选择要运行的命令后,只需按右箭头键,然后按Enter以运行它。

不再需要CTRL + r!如我们所知,我们通过按ctrl + r键在Bash shell的历史记录中搜索命令来进行反向搜索。
但是,没必要在fish shell !由于它具有自动建议功能,因此只需键入命令的前几个字母,然后从历史记录中从已执行的列表中选择命令即可。

智能搜寻

我们还可以进行智能搜索以找到特定的命令,文件或者目录。
例如,我键入命令的子字符串,然后按向下箭头键进入智能搜索,然后再次键入字母以从列表中选择所需的命令。

语法高亮

键入命令时,我们会注意到语法突出显示。
当我在Bash和fish shell中键入相同的命令时,请参见下面的屏幕截图中的区别。

bash:

鱼:

如我们所见,“ sudo”已在鱼shell 中突出显示。
此外,默认情况下,它将以红色显示无效命令。

基于Web的配置

这是鱼shell 的另一个很酷的功能。
我们可以从网页上设置颜色,更改鱼的提示并查看功能,变量,历史记录,键绑定。

要启动Web配置界面,只需键入:

fish_config

手册页补全

Bash和其他shell支持可编程完成,但是只有fish会通过解析已安装的手册页来自动生成它们。

为此,请运行:

fish_update_completions

输出示例为:

Parsing man pages and writing completions to /home/sk/.local/share/fish/generated_completions/
 3435/3435 : zramctl.8.gz

禁用问候语

默认情况下,fish在启动时会向我们打招呼(欢迎钓鱼,它是一个友好的交互式shell)。
如果我们不希望此问候消息,则可以将其禁用。
为此,编辑fish配置文件:

vi ~/.config/fish/config.fish

添加以下行:

set -g -x fish_greeting ''

除了禁用鱼类问候语,我们还可以设置任何自定义问候语消息。

set -g -x fish_greeting 'Welcome to theitroad'

获得帮助

这是另一个令人印象深刻的功能,引起了我的注意。
要从终端在默认的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