Skip to content

Instantly share code, notes, and snippets.

@patorash

patorash/.zshrc

Last active Jul 14, 2020
Embed
What would you like to do?
zsh functions
# ctrl + Rで、pecoでhistoryを参照
function peco-select-history() {
# historyを番号なし、逆順、最初から表示。
# 順番を保持して重複を削除。
# カーソルの左側の文字列をクエリにしてpecoを起動
# \nを改行に変換
BUFFER="$(history -nr 1 | awk '!a[$0]++' | peco --query "$LBUFFER" | sed 's/\\n/\n/')"
CURSOR=$#BUFFER # カーソルを文末に移動
zle -R -c # refresh
}
zle -N peco-select-history
bindkey '^R' peco-select-history
# option + sで、pecoでSSH
function peco-ssh () {
local selected_host=$(awk '
tolower($1)=="host" {
for (i=2; i<=NF; i++) {
if ($i !~ "[*?]") {
print $i
}
}
}
' ~/.ssh/config | sort | peco --query "$LBUFFER")
if [ -n "$selected_host" ]; then
BUFFER="ssh ${selected_host}"
zle accept-line
fi
zle clear-screen
}
zle -N peco-ssh
# Linux
# bindkey '^[s' peco-ssh
# Mac
bindkey 'ß' peco-ssh
# option + Gで、pecoでリポジトリ選択
function peco-ghq-cd () {
local selected_dir=$(ghq list -p | peco --query "$LBUFFER")
if [ -n "$selected_dir" ]; then
BUFFER="cd ${selected_dir}"
zle accept-line
fi
zle clear-screen
}
zle -N peco-ghq-cd
# Linux
# bindkey '^[g' peco-ghq-cd
# Mac
bindkey '©' peco-ghq-cd
# pecoでgit switch
function peco-git-switch () {
local selected_branch=$(git branch | grep -v "*" | peco --query "$LBUFFER" | tr -d ' ')
if [ -n "$selected_branch" ]; then
git switch $selected_branch
fi
}
alias select-branch="peco-git-switch"
# pecoでマージ済のブランチを削除
function peco-git-delete-branch () {
local selected_branch=$(git branch --merged | grep -v "*" | peco --query "$LBUFFER" | tr -d ' ')
if [ -n "$selected_branch" ]; then
git branch -d $selected_branch
fi
}
alias delete-branch="peco-git-delete-branch"
# pecoでkill
function peco-kill () {
local selected_row=$(ps aux | peco --query "$LBUFFER")
if [ -n "$selected_row" ]; then
local pid=$(echo $selected_row | awk '{print $2}')
echo "kill pid: ${pid}. [${selected_row}]"
kill $pid
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.