pecoの簡単で便利な使い方


peco とは

ぐぐりましょう

目的

find . | peco を使って検索したファイル/ディレクトリに cd, ls, rm, cat, vim する。
それぞれエイリアスで簡単に使えるようにする。

こうなる

lsp, vimp, catp なども同様に検索し、それぞれのアクションを行います。

~/.zshrc

bashでも同じかと。

function pecofind() { # find コマンドの出力結果をpeco する
  if [ $# -eq 0 ]; then
    find . | peco
  else
    find $1 | peco
  fi
}
function pecofindd() { # find コマンドの出力結果をpeco する。 ファイルが選ばれたら1つ手前のディレクトリを返す
  if [ $# -eq 0 ]; then
    finded=`find . | peco`
  else
    finded=`find $1 | peco`
  fi
  if [ -f $finded ]; then
    echo $finded | perl -pe 's/\/[^\/]*$/\n/g'
  else
    echo $finded
  fi
}
function cdp() {
  dir=$(pecofindd $1)
  echo "\e[32;1mcd\e[m \e[32;1;4m$dir\e[m"
  cd $dir
}
function lsp() {
  file=$(pecofind $1)
  echo "\e[32;1mls\e[m \e[32;1;4m$file\e[m"
  ls $file
}
function rmp() {
  file=$(pecofind $1)
  echo "\e[32;1mrm\e[m \e[32;1;4m$file\e[m"
  rm $file
}
function treep() {
  file=$(pecofind $1)
  echo "\e[32;1mtree\e[m \e[32;1;4m$file\e[m"
  tree $file
}
function catp() {
  file=$(pecofind $1)
  echo "\e[32;1mcat\e[m \e[32;1;4m$file\e[m"
  cat $file
}
function vimp() {
  file=$(pecofind $1)
  echo "\e[32;1mvim\e[m \e[32;1;4m$file\e[m"
  vim $file
}

注意

find する中身が多いと時間がかかります。(良い手はないか? )

おわり

シンプルかつ便利なpecoの使い方でした

他のpeco の便利系

peco + ghq が便利すぎて、使ってない人を見かけたらすぐに勧めています。
https://qiita.com/strsk/items/9151cef7e68f0746820d