Fish Shell —— 更简单好用的Shell

写在前面

Fish Shell (Friendly Interactive Shell) 是一款现代的Shell工具,它的设计目标是提供更简单、更易用的命令行环境,它具有自动补全、语法高亮和更友好的用户界面等特性。

fish-shell-2024-09-21-19-25-58

Fish Shell (Friendly Interactive Shell)

1.安装最新版本的Fish Shell

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

2.配置默认Shell为Fish Shell

cat /etc/shells # 查看所有Shell
chsh -s /usr/bin/fish 

3.重新打开终端

Starship 轻量、迅速、可无限定制的高颜值终端!

【安装指引】

如果是使用的Windows Terminal等终端,或者是VS Code等,需要注意安装一个 Nerd Font 的字体,并在终端启用(例如,可以尝试使用 Fira Code Nerd Font 字体)。

安装教程: https://zhuanlan.zhihu.com/p/467581369

我这里用的主题是Pastel Powerline Preset

fish-shell-2024-09-21-19-48-59

fzf —— Fish Shell 的理想伙伴

1.首先需要安装fzf

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf # 获取最新版fzf
~/.fzf/install # 安装fzf

2.(可选)使用插件管理器fisher安装fzf.fish

curl -sL https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish | source && fisher install jorgebucaran/fisher # 安装fisher
fisher install jethrokuan/fzf

3.现在就可以通过以下快捷键来操作fzf了

fzf默认快捷键新快捷键备注
Ctrl-tCtrl-o查找文件
Ctrl-rCtrl-r查找历史命令
Alt-cAlt-c打开子目录 (递归搜索)
Alt-Shift-cAlt-Shift-c打开子目录 (隐藏文件夹)
Ctrl-oAlt-o使用默认编辑器 ($EDITOR) 打开文件/目录
Ctrl-gAlt-Shift-o使用xdg-Open或open命令打开文件/目录

4.实现一个快速目录跳转

fzf有非常高的自由度,可以自己写脚本实现很多很有意思的功能,比如实现一个在命令行可以用的目录跳转功能,而不是每次cd一个目录就要ls一下

  1. 创建一个fzf_directory.fish
vim ~/.fzf/shell/fzf_directory.fish

然后写入以下代码

#!/usr/bin/fish

function fzf-getpath --description "Get path to current directory"
    # set -U rpath_cwd_v1 (pwd)

    # 主函数 前进/后退 路径
    # function get_path
    set -l main $argv[1]
    set -l relative_index $argv[2]
    set -l prefix $argv[3]
    if test $main -eq 1
        set -U rpath_cwd_v1 (pwd)
        echo $rpath_cwd_v1
        find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
    else if test $main -eq 0
        if test $prefix != $rpath_cwd_v1
            if test $relative_index -eq 1
                if test $rpath_cwd_v1 = "/"
                    set -U rpath_cwd_v1 /$prefix
                else
                    set -U rpath_cwd_v1 $rpath_cwd_v1/$prefix
                end
                echo $rpath_cwd_v1
                find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
            else if test $relative_index -eq -1
                set -U rpath_cwd_v1 (dirname $rpath_cwd_v1)
                echo $rpath_cwd_v1
                find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
            end
        else
            echo $rpath_cwd_v1
            find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
        end
    else
        set -g rpath_temp $rpath_cwd_v1
        if test -n $prefix && test $prefix != $rpath_cwd_v1
            set -g rpath_temp $rpath_cwd_v1/$prefix
        end
        echo $rpath_temp
    end
end

function fzf-routepath --description "Change directory"
    set -l result (fzf-getpath 1 0 '' | fzf --height=85% --prompt 'Directories> ' --layout=reverse --info=inline --border --margin=1 --padding=1 --preview 'tree -L 2 -C (fish -C "fzf-getpath -1 0 {}")' --bind 'right:reload(fzf-getpath 0 1 {})' --bind 'left:reload(fzf-getpath 0 -1 /)' --bind 'enter:become(fzf-getpath -1 0 {})' --bind 'esc:become(pwd)' )
    if [ -n "$result" ]
        cd -- $result

        # Remove last token from commandline.
        commandline -t ""
        commandline -it -- $prefix
    end

    commandline -f repaint
end

bind \ed fzf-routepath # 绑定fzf-rrpath到Alt-D
  1. 启动Fish Shell 的时候激活

echo "source ~/.fzf/shell/fzf_directory.fish" >> ~/.config/fish/config.fish
source ~/.config/fish/config.fish
  1. 按Alt D,打开fzf页面,按左右键进入和退出目录,按回车cd目录,右侧窗口可以预览目录内容
    fish-shell-2024-09-21-20-38-50

配置好了用起来还是非常爽的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值