LoginSignup
2
4

More than 5 years have passed since last update.

fzfを利用した関数を作るときのテンプレ

Posted at

テンプレート

.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
}
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4