Debin

Debin

工具篇: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

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

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。