🍭

(今さらながら)pecoを使って効率化

に公開
$ brew install peco

履歴から検索してコマンド実行

~/.zshrc
function h() {
  local selected_command=$(fc -l -n 1 | tail -300 | awk '!seen[$0]++' | tac | peco --prompt "HISTORY>")
  
  if [ -n "$selected_command" ]; then
    print -s "$selected_command"
    echo "Executing: $selected_command"    
    eval "$selected_command"
  fi
}

※ tacコマンドが使えない場合にはbrew install coreutilsを実行。

履歴から検索してディレクトリ移動

~/.zshrc
DIRSTACKFILE="$HOME/.cache/zsh/dirs"
if [ ! -d "$(dirname $DIRSTACKFILE)" ]; then
  mkdir -p "$(dirname $DIRSTACKFILE)"
fi
if [ -f $DIRSTACKFILE ] && [ $#dirstack -eq 0 ]; then
  dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
  for dir in $dirstack; do
    if [ -d "$dir" ]; then
      cd "$dir"
    fi
  done
fi
function chpwd() {
  print -l $PWD ${(u)dirstack} >! $DIRSTACKFILE
}
function c() {
  local dir=$(dirs -v | awk '!seen[$2]++' | peco | awk '{print $2}')

  if [ -n "$dir" ]; then
    dir=${dir/#\~/$HOME}
    cd "$dir"
  fi
}
DIRSTACKSIZE=100
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_SILENT 

Discussion