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
  1. 配置默認 Shell 為 Fish Shell
cat /etc/shells # 查看所有Shell
chsh -s /usr/bin/fish 
  1. 重新打開終端

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
  1. (可選) 使用插件管理器 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
  1. 現在就可以通過以下快捷鍵來操作 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 命令打開文件 / 目錄
  1. 實現一個快速目錄跳轉

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

配置好了用起來還是非常爽的。

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。