Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
93 lines (71 sloc) 2.16 KB
#!/bin/bash
# https://github.com/junegunn/fzf
# brew install fzf
# fzf style
export FZF_DEFAULT_OPTS='--height 40% --reverse'
# respect gitignore and use git ls-tree in git repos
export FZF_DEFAULT_COMMAND='
(git ls-tree -r --name-only HEAD ||
find . -path "*/\.*" -prune -o -type f -print -o -type l -print |
sed s/^..//) 2> /dev/null'
# helper to echo and execute a command
function _echo_and_execute() {
echo $1
eval $1
}
## History
# search history and re-run, filtering first with grep on arg, then with fzf; e.g
function hist(){
$(history | tail -r | grep -iE "$*" | fzf | xargs | cut -d ' ' -f 2- | xargs | tr -d '\n' | pbcopy)
}
## PIDs
# fzf a PID
function _fzf_pid() {
echo -n $(ps axww -o pid,user,%cpu,%mem,start,time,command | fzf | sed 's/^ *//' | cut -f1 -d' ')
}
# copy PID to clipboard
function pid() {
pid=$(_fzf_pid)
echo -n "$pid" | pbcopy
}
## GIT
# fzf a local branch, trims leading whitespace, and "*"
function _fzf_git_branch() {
echo $(git branch | sed 's/^. //' | fzf | xargs echo)
}
# choose branch and pass it to `command`, or copy the branch name if no args
# USAGE: gbp [command]
function gbp() {
branch=$(_fzf_git_branch)
if [ $# -eq 0 ]; then
echo -n $branch | pbcopy; return;
fi
_echo_and_execute "$* $branch"
}
# `git branch checkout`
alias gbc="gbp git co"
# `git branch delete
alias gbd="gbp git br -D"
## Rails
# fzf (with preview) a rails migration and pass it as the command VERSION
function _fzf_rails_migration() {
echo $(ls ./db/migrate/ | fzf --preview 'head -20 ./db/migrate/{}' | xargs | cut -d '_' -f 1 | xargs | tr -d '\n')
}
function rdbm() {
if [ ! -d "./db/migrate" ]; then
echo "Opps, you're not in a Rails directory"
else
if [ $# -eq 0 ]; then
_echo_and_execute "bundle exec rake db:migrate"
else
migrate_version=$(_fzf_rails_migration)
if [ ! -z $migrate_version ]; then
_echo_and_execute "$*$migrate_version"
fi
fi
fi
}
# db migrate up/down/redo with VERSION
alias rdbmu="rdbm bundle exec rake db:migrate:up VERSION="
alias rdbmd="rdbm bundle exec rake db:migrate:down VERSION="
alias rdbmr="rdbm bundle exec rake db:migrate:redo VERSION="