wada811 / blog

wada811's blog
https://github.com/wada811/blog/issues
Apache License 2.0
6 stars 0 forks source link

peco で開いたディレクトリの履歴からディレクトリを開く #13

Open wada811 opened 5 years ago

wada811 commented 5 years ago

Why

ディレクトリの履歴から絞り込んでディレクトリを開きたい。

What

zsh の組み込みの cdr コマンドでディレクトリの履歴から peco で絞り込む。

How

~/.zshrc に以下を設定する。

add-zsh-hook を有効にする

autoload -Uz add-zsh-hook

cdr を有効にする

autoload -Uz chpwd_recent_dirs cdr
add-zsh-hook chpwd chpwd_recent_dirs

cdr の設定

zstyle ':completion:*' recent-dirs-insert both
zstyle ':chpwd:*' recent-dirs-max 500
zstyle ':chpwd:*' recent-dirs-default true
zstyle ':chpwd:*' recent-dirs-file "$HOME/.cache/shell/chpwd-recent-dirs"
zstyle ':chpwd:*' recent-dirs-pushd true
mkdir -p ~/.cache/shell/

peco の設定

function peco-cdr() {
    local selected_dir=$(cdr -l | awk '{ print $2 }' | peco)
    if [ -n "$selected_dir" ]; then
        BUFFER="cd ${selected_dir}"
        zle accept-line
    fi
    zle clear-screen
}
zle -N peco-cdr

# 履歴から cd
bindkey '^@' peco-cdr

Ref