テンプレート
.bash_profile
などに追記してください。
bash_profile
function-name() {
# [get_list_command]にはリストを表示するコマンドを書く
# e.g.) git branch
# print以降はスペース区切りで何個目の要素を表示するか
# 行全体を表示する場合は$0を書く
local line="$([get_list_command] | fzf --height 40% | awk '{print $1}')"
if [ -n "${line}" ]; then
echo 'wait...'
# [do_command]には選択された行に対して行うコマンドを書く
# e.g.) git checkout ${line}
[do_command] ${line}
fi
}
options
ヘッダを消したい場合
[get_list_command]の後に sed -e '1d'
を挟む
.bash_profile
line="$([get_list_command] | sed -e '1d' | fzf --height 40% | awk '{print $1}')"
区切り文字を変えたい場合
awkに-F 'hoge'
を追加する
line="$([get_list_command] | fzf --height 40% | awk -F ':' '{print $1}')"
具体例
gitのbranchを取得して、選択したブランチをcheckoutする
.bash_profile
git-checkout() {
local list="$(git branch | fzf --height 40% | awk '{print $0}')"
if [ -n "${list}" ]; then
echo 'wait...'
git checkout ${list}
fi
}