安装Fish自动匹配历史命令

本文介绍了Fish的使用,使用Fish可以根据输入自动匹配历史命令。

摘要:Fish的官网宣传语是 Finally, a command line shell for the 90s。 翻译过来就是 Fish shell 是一个为90后准备的 shell。有人说:“二逼青年用bash,普通青年用zsh,文艺青年用fish。” 其次由于zsh 的速度实在是太慢,所以决定换用fish,fish速度快,智能提示强大。

关键字:Fish

Fish入门使用

Ubuntu安装Fish
1
2
3
4
5
6
7
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

fish的鲜明特征在于安装时已经默认集成了很多需要的功能。 比如:

  • 命令行语法高亮,错误会显示红色
  • 智能提示
  • 可以使用web网页的进行终端配置

fish 有智能提示,一个命令一旦输入过一次,会自动显示上一次的全部命令,细心一点会发现会有一层灰色的字体表示上一次的命令,按Ctrl+F或者 右方向键, 即可自动补全。

网页配置Fish

fish_config 可以直接跳出网页版本配置fish的界面。

web版本可以设置主题, 推荐其中的"Tomorrow Night"主题颜色。

选择想要的主题,然后点击set theme即可设置主题。 在命令里按enter 即可退出web版本的界面。

在prompt里面可以自己选择fish终端的主题。

兼容Bash

由于fish 很多不兼容bash的功能导致了很多脚本无法运行,这一点是很多人吐槽fish的地方,我们需要一种方式来运行bash脚本。

比如

1
arc land --onto `git rev-parse --abbrev-ref HEAD` 

只需要在前面添加一个bash -c 命令即可。

1
bash -c "arc land --onto `git rev-parse --abbrev-ref HEAD`"

顺手加个alias就更方便了,可以直接在命令行里使用命令arcl

1
alias arcl bash -c "arc land --onto `git rev-parse --abbrev-ref HEAD`"

对于脚本文件,比如我将需要执行的命令或文件放到repomerge.sh

在~/.config/fish/config.fish添加

1
alias up "bash -c /usr/bin/repomerge.sh"

然后就可以自由的使用up命令了

其中function fish_prompt 函数用于定义fish终端的显示样式。

我们只需要写一个fish_prompt函数即可。集成了git的分支名称以及当前的变化。

显示的样式如下:

说明: ✔代表当前git项目是干净的。 %1 表示有一个文件未追踪 +1 表示一个文件已暂存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 终端显示样式的配置
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

if git_is_repo
if not set -q __git_cb
set __git_cb (set_color blue)" ("(set_color brred)(git branch | grep \* | sed 's/* //') (set_color -o bryellow)(__fish_git_prompt_informative_status)(set_color blue)")"
end
end


if not set -q __fish_prompt_cwd
set -g __fish_prompt_cwd (set_color $fish_color_cwd)
end
printf '%s%s%s%s ' "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" $__git_cb
end

隐藏欢迎语

在confin.sh文件里添加如下函数即可

1
2
function fish_greeting
end

其他配置

1
2
3
4
alias l "ll"
alias dir "dde-file-manager . &"
alias docker "sudo docker"
alias apt "sudo apt"