Skip to content

Instantly share code, notes, and snippets.

@alice1017
Last active July 20, 2018 06:41
Show Gist options
  • Save alice1017/0d02e29101e6a9e4fda72090601ad052 to your computer and use it in GitHub Desktop.
Save alice1017/0d02e29101e6a9e4fda72090601ad052 to your computer and use it in GitHub Desktop.
zsh funcitons
# - Functions
# pkill: Kill the process of grepped
pkill() {
local target="$1"
pgrep "$target" | xargs -iproc kill proc > /dev/null 2>&1
return $?
}
# check_peco_exists: check the peco binary exists or not
check_peco_exists() {
which peco > /dev/null 2>&1
if [ ! "$?" = "0" ];then
echo "The peco binary don't exists." 1>&2
exit 1
else
return 0
fi
}
# back: search a command from history by peco, and eval it
back() {
local result
local command
check_peco_exists
result="$(history -i 1 | sort --reverse | peco)"
command="${result:25}"
echo "$command"
eval "$command"
return $?
}
alias !="back"
# pd: move current directory to searched directory contains '.git' dir
pd() {
local result # search reuslt by peco
local dest # destination directory
local command # eval command
check_peco_exists
result="$(find . -type d -name .git | peco)"
dest="$(dirname $result)"
command="cd $dest"
echo "$dest"
eval $command
}
# fd: move current directory to searched directory by `find .`
# ignored dirs: .git .sass-cache
fd() {
local dest # destination directory
local command # eval command
check_peco_exists
dest="$(find . -type d \
| grep -v .git \
| grep -v .sass-cache \
| peco)"
echo "$dest"
command="cd $dest"
eval $command
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment