LoginSignup
0
1

More than 5 years have passed since last update.

pecoやfzfで実行したコマンドを履歴に残す

Posted at

pecofzfなどのインタラクティブフィルターCUIツールを使うときに、履歴に残らないのが結構不便な時がある。例えば、fzfを使ってgitのレポジトリのブランチのログを見るとする。

git log `git branch | fzf`

だが、これだとfzf実行後のコマンドではなく、上記のコマンドがそのまま履歴に残ってしまう。

$ history -1

 6439  git log `git branch | fzf`

ここで、fzf実行後のgit log (fzf実行後のブランチ名)の履歴を残して欲しい。なので、履歴に残すようにした。

環境は
Mac OS X Sierra 10.12.5
zsh 5.3.1 (x86_64-apple-darwin14.5.0)

以下のコマンドを実装した。

# コマンドを履歴に残す
function save_history(){
  local cmd="$@"
  eval $cmd
  if test $? -ne 0
  then
    # 失敗したらそのまま終了
    return 1
  fi
  # 成功したら履歴に保存
  print -s $cmd
}

上記のコマンドをpecoやfzfを使ったコマンドの前で呼ぶだけ。

実行例

save_history git log `git branch | fzf`

これでfzf後の履歴も残るようになった。

$ history -2

 6453  save_history git log `git branch | fzf`
 6454  git log (fzf実行後のブランチ名)

(でも、エイリアスとか関数で設定しないで使うのはめんどくさい。もっといい方法あるのかな...?)

0
1
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
0
1