Debin

Debin

工具篇:Fish Shell —— より簡単で使いやすいシェル

Fish Shell (Friendly Interactive 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. デフォルトシェルを Fish Shell に設定する
cat /etc/shells # すべてのシェルを表示
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-oxdg-open または open コマンドでファイル / ディレクトリを開く
  1. 高速ディレクトリジャンプを実現する

fzf は非常に自由度が高く、スクリプトを自分で書いて多くの面白い機能を実現できます。例えば、コマンドラインで使用できるディレクトリジャンプ機能を実装することができます。毎回 cd コマンドを使ってディレクトリを移動するのではなく、ls コマンドを使う必要がなくなります。

  1. fzf_directory.fish を作成する
vim ~/.fzf/shell/fzf_directory.fish

次に以下のコードを書き込みます

#!/usr/bin/fish

function fzf-getpath --description "現在のディレクトリへのパスを取得"
    # 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 "ディレクトリを変更"
    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

        # コマンドラインから最後のトークンを削除します。
        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 ページが開き、左右キーでディレクトリに入ったり出たりし、Enter キーでディレクトリに cd し、右側のウィンドウでディレクトリの内容をプレビューできます。
    fish-shell-2024-09-21-20-38-50

設定が完了すると、非常に快適に使用できます。

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。