Closed17
fish, fisher, fzf を使ってターミナルをカスタムしよう
まとめ
- ターミナルをカスタムしたくなった
- bash や zsh を長らく使用してきたけど、fish に入門しようと思った
- シェルの設定スクリプトも見直したくなった
前提条件
- macOS, Apple M2 Pro
Shell
基本的な設定をしたら、あとは fisher でいい感じのを追加する。
fish のインストール
# brew でインストール
brew install fish
# Fish Shellの実行ファイルのパスを確認
which fish
# Fishのパスを追加
echo "$(which fish)" | sudo tee -a /etc/shells
# Fishをデフォルトシェルに変更
chsh -s "$(which fish)"
設定
基本設定
~/.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
fish_config
で UI をお好みに変更
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://github.com/yuys13/fish-cdf
fisher install yuys13/fish-cdf
cf. https://github.com/yuys13/fish-autols
fisher install yuys13/fish-autols
cf. https://github.com/yuys13/fish-ghq-fzf
fisher install yuys13/fish-ghq-fzf
次のような感じでカスタムすると README のプレビューも表示できる。
fzf --preview "bat --color=always --style=header,grid --line-range :80 {}/README.*
cf. https://qiita.com/tomoyamachi/items/e51d2906a5bb24cf1684
ついでに --reverse
も入れよう
cf. https://github.com/yuys13/fish-fzf-bd
fisher install yuys13/fish-fzf-bd
cf. https://github.com/yuys13/fish-gcd
fisher install yuys13/fish-gcd
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
cf. https://github.com/wfxr/forgit
fisher install wfxr/forgit
Terminal から Web Browser の Bookmark を操作する
~/.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ヶ月前にクローズされました