Closed17

fish, fisher, fzf を使ってターミナルをカスタムしよう

anfangdanfangd

まとめ

  • ターミナルをカスタムしたくなった
  • bash や zsh を長らく使用してきたけど、fish に入門しようと思った
  • シェルの設定スクリプトも見直したくなった

前提条件

  • macOS, Apple M2 Pro
anfangdanfangd

Shell

基本的な設定をしたら、あとは fisher でいい感じのを追加する。

anfangdanfangd

fish のインストール

# brew でインストール
brew install fish

# Fish Shellの実行ファイルのパスを確認
which fish

# Fishのパスを追加
echo "$(which fish)" | sudo tee -a /etc/shells

# Fishをデフォルトシェルに変更
chsh -s "$(which fish)"

anfangdanfangd

設定

基本設定

~/.config/fish/config.fish
if status is-interactive
    # Commands to run in interactive sessions can go here
end

# 🛠️ PATH 設定
set -gx PATH /opt/homebrew/bin $PATH

# 🚀 FZF
set -Ux FZF_DEFAULT_COMMAND "fd --type f"
set -Ux FZF_CTRL_T_COMMAND "fd --type f"
set -Ux FZF_ALT_C_COMMAND "fd --type d"

# 🌟 Starship プロンプト
starship init fish | source

# ⚡ Zoxide(高速ディレクトリ移動)
zoxide init fish | source

# 🎮 Vim キーバインド
fish_vi_key_bindings

# Go 設定
set -Ux GOPATH "$HOME/go"
set -Ux PATH "$GOPATH/bin" $PATH

エイリアス

~/.config/fish/conf.d/aliases.fish
# list
abbr -a ls "lsd"
abbr -a l "lsd -l"
abbr -a ll "lsd -l"
abbr -a la "lsd -a"
abbr -a lla "lsd -la"
abbr -a lt "lsd --tree --classic"

abbr -a codei "code-insiders"

# 🐋 Docker
abbr -a dkr "docker"
abbr -a dkrc "docker-compose"

# 🐙 Git
abbr -a gb  "git branch -vv"
abbr -a gba "git branch -a -v"
abbr -a gsw "git switch"
abbr -a gcm "git commit -m"
abbr -a gd  "git diff"
abbr -a gf  "git fetch"
abbr -a gpl "git pull"
abbr -a gs  "git status"

# diff(colordiff が使える場合)
if type -q colordiff
    abbr -a diff "colordiff -u"
else
    abbr -a diff "diff -u"
end

~/.config/fish/functions/fish_greeting.fish
function fish_greeting
    echo ""
    echo "🐟 Welcome to "(set_color cyan)"Fish Shell"(set_color normal)"! 🐟"
    echo '🐟   version:' $version
    printf "
  %sCommands%s:
    - cdf  : Go to the frequently used directory
    - bd   : Go back to the Parent Directory
    - gcd  : Go to the root directory of the current Git repository
    - z    : Go to the frequently used directory
    - fd   : Find files and directories
    - rg   : Search for text in files
    - ghq  : Search and open GHQ repositories
    - fzf  : Fuzzy search for files and directories
    - bat  : Display the contents of a file with syntax highlighting
    - lsd  : List directories
    - delta: Show git diff with syntax highlighting
    - mise : Manage environment of the various tools
    - gh   : GitHub CLI
    - vhs  : Record the operation history of the terminal
    - xh   : Friendly and fast tool for sending HTTP requests" (set_color cyan) (set_color normal)
    echo ""
    printf "
  %sKeybindings%s:
    - Ctrl + G: Search GHQ repositories
    - Ctrl + R: Search command history
    - Ctrl + F: Search files and directories
    - Ctrl + L: Search Git logs
    - Ctrl + S: Search Git status
    - Ctrl + P: Search processes
    - Ctrl + V: Search environment variables" (set_color cyan) (set_color normal)
    echo ""
    echo ""
    echo "🚀 Type "(set_color green)"help"(set_color normal)" for instructions on how to use fish"

end

anfangdanfangd

fisher

cf. https://github.com/jorgebucaran/fisher

curl -sL https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish | source && fisher install jorgebucaran/fisher

cf. https://zenn.dev/yuys13/articles/fish-plugins-2025

anfangdanfangd

cf. https://github.com/PatrickF1/fzf.fish

fisher install PatrickF1/fzf.fish

Escape が押しづらいので消した

~/.config/fish/functions/fzf_configure_bindings.fish
        # Initialize with default key sequences and then override or disable them based on flags
        # index 1 = directory, 2 = git_log, 3 = git_status, 4 = history, 5 = processes, 6 = variables
        # set -f key_sequences \e\cf \e\cl \e\cs \cr \e\cp \cv # \c = control, \e = escape
        set -f key_sequences \cf \cl \cs \cr \cp \cv # \c = control, \e = escape
anfangdanfangd

Terminal から Web Browser の Bookmark を操作する

anfangdanfangd
~/.config/fish/functions/chrome-bookmarks.fish
function chrome-bookmarks
    set bookmark_file "$HOME/Library/Application Support/Google/Chrome/Default/Bookmarks"
    if test -f $bookmark_file
        set selected (jq -r '.roots.bookmark_bar.children[] | "\(.name) \(.url)"' $bookmark_file | fzf | awk '{print $NF}')
        if test -n "$selected"
            open "$selected"
        end
    else
        echo "Chrome ブックマークファイルが見つかりません。" >&2
    end
end

~/.config/fish/conf.d/chrome-bookmarks.fish
bind \cb chrome-bookmarks
bind -M insert \cb chrome-bookmarks

このスクラップは2ヶ月前にクローズされました