Update fzf functions and add alias

This commit is contained in:
drymer 2018-05-06 20:37:32 +02:00
parent 2ccb43af24
commit 3862042a06
3 changed files with 134 additions and 16 deletions

View File

@ -1,5 +1,5 @@
#+startup: indent
* Proveerdor de entorno
* Proveedor de entorno
Este playbook instala lo siguiente:
- Docker

View File

@ -13,3 +13,8 @@ alias cdtemp="cd `mktemp -d`"
alias gitinit="git config user.email drymer@autistici.org; git config user.name drymer; git config commit.gpgsign true"
alias ytv="cd ~/.syncthing/VideosMovil; torify youtube-dl -f 18 "
alias h='function hdi(){ howdoi $* -n 3; }; hdi'
alias watch='watch -n1'
alias scrcpy='docker run -ti --privileged -v /dev/bus/usb:/dev/bus/usb -v ~/.android:/root/.android -v /tmp/.X12-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY -p 5037:5037 -v $HOME/.Xauthority:/root/.Xauthority --net=host scrcpy'
alias cwd='pwd | tr -d "\r\n" | xclip -selection clipboard'
alias sniff="sudo ngrep -d 'en1' -t '^(GET|POST) ' 'tcp and port 80'"
alias httpdump="sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E \"Host\\: .*|GET \\/.*\""

View File

@ -146,17 +146,6 @@ function helm (){
}
# fzf functions
## fkill - kill process
fkill() {
local pid
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
}
## insert located file
fzf-locate-widget() {
local selected
@ -174,8 +163,12 @@ runc() {
local image=$(docker images --format '{{.Repository}}:{{.Tag}}' | fzf-tmux --reverse --multi)
if [[ $image != '' ]]; then
echo -e "\n \033[1mDocker image:\033[0m" $image
read -e -p $' \e[1mOptions: \e[0m' -i "-it --rm" options
read -rs $'opt? \e[1mOptions: \e[0m'
# TODO: No lo coge con -z aun estando vacio
if [[ -z $opt ]]
then
opt="-it --rm"
fi
printf " \033[1mChoose the command: \033[0m"
local cmd=$(echo -e "/bin/bash\nsh" | fzf-tmux --reverse --multi)
if [[ $cmd == '' ]]; then
@ -199,7 +192,7 @@ runc() {
export FZF_DEFAULT_OPTS=""
history -s runc
history -s docker run $options -v $volume $image $cmd
history -s docker run $opt -v $volume $image $cmd
echo ''
docker run $options -v $volume $image $cmd
fi
@ -268,8 +261,128 @@ showipc() {
fi
}
# Clean docker
## Git stuff
# fbr - checkout git branch (including remote branches), sorted by most recent commit, limit 30 last branches
unalias gbr
gbr() {
local branches branch
branches=$(git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format="%(refname:short)") &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
# fco - checkout git branch/tag
unalias gco
gco() {
local tags branches target
tags=$(
git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
branches=$(
git branch --all | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$(
(echo "$tags"; echo "$branches") |
fzf-tmux -l30 -- --no-hscroll --ansi +m -d "\t" -n 2) || return
git checkout $(echo "$target" | awk '{print $2}')
}
# fco_preview - checkout git branch/tag, with a preview showing the commits between the tag/branch and HEAD
gco_preview() {
local tags branches target
tags=$(
git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
branches=$(
git branch --all | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$(
(echo "$tags"; echo "$branches") |
fzf --no-hscroll --no-multi --delimiter="\t" -n 2 \
--ansi --preview="git log -200 --pretty=format:%s $(echo {+2..} | sed 's/$/../' )" ) || return
git checkout $(echo "$target" | awk '{print $2}')
}
# fcoc - checkout git commit
fcoc() {
local commits commit
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# fshow - git commit browser
fshow() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}
alias glNoGraph='git log --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr% C(auto)%an" "$@"'
local _gitLogLineToHash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
local _viewGitLogLine="$_gitLogLineToHash | xargs -I % sh -c 'git show --color=always % | diff-so-fancy'"
# fcoc_preview - checkout git commit with previews
fcoc_preview() {
local commit
commit=$( glNoGraph |
fzf --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview $_viewGitLogLine ) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# fshow_preview - git commit browser with previews
fshow_preview() {
glNoGraph |
fzf --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview $_viewGitLogLine \
--header "enter to view, alt-y to copy hash" \
--bind "enter:execute:$_viewGitLogLine | less -R" \
--bind "alt-y:execute:$_gitLogLineToHash | xclip"
}
# fcs - get git commit sha
# example usage: git rebase -i `fcs`
fcs() {
local commits commit
commits=$(git log --color=always --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e --ansi --reverse) &&
echo -n $(echo "$commit" | sed "s/ .*//")
}
# fuzzy grep open via rg
vg() {
local file
file="$(ag --nobreak --noheading $@ | fzf -0 -1 | awk -F: '{print $1 " +" $2}')"
if [[ -n $file ]]
then
vim $file
fi
}
# fd - cd to selected directory
fd() {
local dir
dir=$(find ${1:-.} -path '*/\.*' -prune \
-o -type d -print 2> /dev/null | fzf +m) &&
cd "$dir"
}
# like normal z when used with arguments but displays an fzf prompt when used without.
unalias z 2> /dev/null
z() {
[ $# -gt 0 ] && _z "$*" && return
cd "$(_z -l 2>&1 | fzf --height 40% --nth 2.. --reverse --inline-info +s --tac --query "${*##-* }" | sed 's/^[0-9,.]* *//')"
}
# Clean docker
function docker_clean(){
echo "Deleting stopped containers..."
docker rm $(docker ps -a -q | awk '{print $1}')