Skip to content

Instantly share code, notes, and snippets.

@mizdra
Last active October 20, 2021 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizdra/0c8c88cc24e10944ebee4d40ac038633 to your computer and use it in GitHub Desktop.
Save mizdra/0c8c88cc24e10944ebee4d40ac038633 to your computer and use it in GitHub Desktop.
########################################
## peco
## (ref http://k0kubun.hatenablog.com/entry/2014/07/06/033336)
########################################
export GHQ_ROOT="$HOME/src"
# `cd ~gomi` でゴミ置き場に移動できるように
hash -d gomi=~/src/localhost/gomi
alias src-list="find ~/src -mindepth 3 -maxdepth 3 -type d | sed -e \"s|$HOME/src/||g\""
function peco-src() {
# ディレクトリ構成
# ~/src
# ├── gist.github.com
# │   └── mizdra
# │      └── repository-1
# ├── github.com
# │   ├── RNGeek
# │   │   └── repository-1
# │   └── mizdra
# │      ├── repository-1
# │      └── repository-2
# └── localhost
#    ├── uec
#    │   ├── temp-repository-1
#    │   └── temp-repository-2
#    └── gomi
#       ├── temp-repository-1
#      └── temp-repository-2
local selected_dir=$(src-list | peco --query "$LBUFFER")
if [ -n "$selected_dir" ]; then
BUFFER="cd ${GHQ_ROOT}/${selected_dir}"
zle accept-line
fi
zle redisplay
}
zle -N peco-src
stty -ixon
bindkey '^s' peco-src
function peco-select-history() {
typeset tac
if which tac > /dev/null; then
tac=tac
else
tac='tail -r'
fi
BUFFER=$(fc -l -n 1 | eval $tac | peco --query "$LBUFFER")
CURSOR=$#BUFFER
zle redisplay
}
zle -N peco-select-history
bindkey '^r' peco-select-history
function peco-select-branch () {
local branch=$(git branch -v --sort=-authordate | peco | cut -c 3- | awk '{ print $1 }')
if [ -n "$branch" ]; then
if [ -n "$LBUFFER" ]; then
local new_left="${LBUFFER%\ } $branch"
else
local new_left="$branch"
fi
BUFFER=${new_left}${RBUFFER}
CURSOR=${#new_left}
fi
}
zle -N peco-select-branch
bindkey '^b' peco-select-branch
function peco-find-file() {
if git rev-parse 2> /dev/null; then
source_files=$(git ls-files)
else
source_files=$(find . -type f)
fi
selected_files=$(echo $source_files | peco --prompt "[find file]")
result=''
for file in $selected_files; do
result="${result}$(echo $file | tr '\n' ' ')"
done
BUFFER="${BUFFER}${result}"
CURSOR=$#BUFFER
zle redisplay
}
zle -N peco-find-file
bindkey '^f' peco-find-file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment