fish shell 简要教程以及对bash的兼容性讨论。

[TOC]html

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

本文的亮点在于三点:linux

一、Fish的入门使用
二、与bash兼容性的方案
三、一个属于本身的Fish主题git

Fish配置请参考:https://github.com/iceqing/li...github

下面分别介绍web

1、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]](https://fishshell.com/)
fish的鲜明特征在于安装时已经默认集成了不少须要的功能。
好比:docker

  • 命令行语法高亮,错误会显示红色

  • 智能提示
  • 可使用web网页的进行终端配置

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

智能提示

二、 安装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

三、网页配置fish

fish_config 能够直接跳出网页版本配置fish的界面。
web版本能够设置主题, 推荐其中的"Tomorrow Night"主题颜色。ubuntu

image.png

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

在prompt里面能够本身选择fish终端的主题。
fish终端的主题bash

四、插件管理

https://github.com/oh-my-fish...

omf install thefuck

虽然有fisher这个管理工具,可是目前还不稳定。

五、git 配置

咱们只须要配置alias 既能够知足平常git命令的使用。对于终端主题样式,咱们能够自行进行配置。(后边有介绍)

# git 相关的配置
alias g "git"
alias gst "git status"
alias gb "git branch"
alias gba "git branch -a" 
alias gl "git pull"

2、兼容bash脚本

因为fish 不少不兼容bash的功能致使了不少脚本没法运行,这一点是不少人吐槽fish的地方,咱们须要一种方式来运行bash脚本。

好比

echo hello && echo world

这条命令在fish里没法执行。
咱们只须要在前面添加一个bash -c 命令便可,以下所示。

bash -c "echo hello && echo world"

或者将一下内容

echo hello && echo world

放入hello.sh文件中

顺手加个alias就更方便了,能够直接在命令行里使用命令hello

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

alias hello bash -c "echo hello && echo world"
或者
alias hello "bash -c /usr/bin/hello.sh"

而后就能够自由的使用hello命令了

注意:在3.0.0中fish已经兼容了&&,||,!语法

3、配置本身的终端

样式显示以下:

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

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

显示的样式以下:

image.png

**说明:
✔表明当前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

    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

1. 隐藏欢迎语

在confin.sh文件里添加以下函数便可

function fish_greeting
end

2. 其余配置

alias l "ll"
# 列出所有文件包含隐藏文件
alias la "ls -a"
alias apt "sudo apt"

3. 所有配置以下

配置的仓库地址:https://github.com/iceqing/ic...

# git配置
alias g "git"
alias gst "git status"# 将此文件内容复制到到 ~/.config/fish/config.fish后生效
# 一、git配置
abbr g "git"
abbr gb "git branch"
abbr gba "git branch -a"
abbr gco "git checkout"
abbr gst "git status"
abbr gm "git commit -m "
abbr gam "git commit -am "
abbr gl "git pull"
abbr gp "git push"

# 二、须要安装 trash-cli,防止误删文件
abbr tl "trash-list"
abbr tr "trash-restore"
abbr t "trash"
abbr rm "trash"
alias rm "echo -e 'Can not use the command: `rm`, may be you will need: trash'"

# 三、其余配置
alias l "ll"
# 清除屏幕
alias c "clear"
# 安装文件
alias apt "sudo apt"
# 移除终端提示语配置
set fish_greeting
# autojump配置文件
if test -f /usr/share/autojump/autojump.fish; . /usr/share/autojump/autojump.fish; end

# 四、自定义Fish Shell终端显示样式的配置
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 is_git_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%s ' "$USER" "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" $__git_cb
end

# 判断是不是git仓库的工具函数
function is_git_repo -d "Check if directory is a repository"
  test -d .git; or command git rev-parse --git-dir >/dev/null ^/dev/null
end
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 >/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



# 终端提示语配置
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

参考

  1. 官网
  2. fish 安装 https://launchpad.net/~fish-s...
  3. Fish shell 入门教程 (阮一峰) http://www.ruanyifeng.com/blo...
  4. 宇宙第一shell——fish入门 https://www.jianshu.com/p/7ff...

本文采用署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)协议,转载请注明出处。

系列文章

1. Fish Shell使用心得
2. Fish Shell 3.0 新功能

相关文章
相关标签/搜索