functions (6623B)
1 #!/usr/bin/env bash 2 # vim:foldmethod=indent:foldlevel=0 3 cd() { builtin cd -P "$@" || return; } # list contents after cding 4 mkcd() { mkdir -p -- "$1" && cd -P -- "$1" || return; } # make dir and cd at the same time 5 procinfo() { pgrep -f "$1"; } # get info about a process (by name) 6 rmempty() { find "${1-.}" -type d -empty -depth -delete; } # remove empty directories 7 hr() { : $((i=0)); while [ $((i<$(tput cols))) -ne 0 ]; do printf "#"; _=$((i=i+1)); done; echo; } # insert a <hr> in the terminal 8 vimq() { vim -q <(eval "$(fc -nl -1)"); } # load results of last command (often ag) in vim quickfix. eval necessary to handle escaped quotes. 9 gpip(){ PIP_REQUIRE_VIRTUALENV="" pip "$@"; } # Access global pip while using virtualenvs 10 nls() { printf $((`ls -l | wc -l | xargs echo` - 1)); } # Count the number of files in the directory 11 reload-completions() { rm ~/.zcompdump && compinit; } 12 zp() { z -l "$@" | awk -F' ' 'END {printf $2}'; } # Get the result of a "z" comman 13 reload() { 14 local START_TIME=$SECONDS 15 if [[ $SHELL = *"zsh"* ]]; then 16 source "$HOME"/.zshrc 17 elif [[ $SHELL = *"bash"* ]]; then 18 source "$HOME"/.bash_profile 19 fi 20 echo "Reloaded config ($(( SECONDS-START_TIME )) sec)." 21 } 22 extract () { 23 if [ -f "$1" ] ; then 24 case $1 in 25 *.tar.bz2) tar xjf "$1" ;; 26 *.tar.gz) tar xzf "$1" ;; 27 *.bz2) bunzip2 "$1" ;; 28 *.rar) unrar e "$1" ;; 29 *.gz) gunzip "$1" ;; 30 *.tar) tar xf "$1" ;; 31 *.tbz2) tar xjf "$1" ;; 32 *.tgz) tar xzf "$1" ;; 33 *.zip) unzip "$1" ;; 34 *.Z) uncompress "$1" ;; 35 *.7z) 7z x "$1" ;; 36 *) echo "'$1' cannot be extracted via extract()" ;; 37 esac 38 else 39 echo "'$1' is not a valid file" 40 fi 41 } 42 43 # Docker functions 44 if command -v docker &>/dev/null; then 45 eslint_here() { 46 docker run -it --rm -v $(pwd):/data cytopia/eslint . -f unix; 47 } 48 fi 49 50 # Applescript-reliant functions 51 if command -v osascript &> /dev/null; then 52 # cd to current Finder directory 53 cdf() { 54 target="$(osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)')" 55 if [ -n "$target" ]; then 56 cd "$target" || printf "Could not cd to %s.\n" >&2 57 pwd 58 else 59 printf 'No Finder window found.\n' >&2 60 fi 61 } 62 63 # echo current Finder directory 64 gdf() { 65 osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)' 66 } 67 # echo selected Finder file 68 gsf() { 69 osascript -e 'tell application "Finder" to return POSIX path of (first item of (get selection as alias list) as alias)' | rev | sed 's/^\///' | rev; 70 } 71 fi 72 73 if command -v qlmanage &> /dev/null; then 74 qlf() { qlmanage -p "$@" &> /dev/null; } 75 fi 76 77 if command -v virtualenv &> /dev/null; then 78 # Useful if want to cd without running any virtualenvwrapper hooks 79 workoncd() { 80 [ $# -eq 1 ] || { echo 'Must provide name of virtualenv.'; return; } 81 [ -z "$WORKON_HOME" ] && { echo '$WORKON_HOME not set, exiting.'; return; } 82 [ -d "$WORKON_HOME/$1" ] || { echo "Virtual environment $1 not found"; return; } 83 source "$WORKON_HOME/$1/bin/activate" && cdproject 84 } 85 fi 86 # fzf-reliant functions 87 if command -v fzf >/dev/null 2>&1; then 88 { [ -f ~/.config/dark-theme ] && export BAT_THEME='1337'; } || export BAT_THEME='GitHub' 89 export FZF_DEFAULT_OPTS="--preview-window right:100:hidden:wrap --bind '?:toggle-preview' --preview='(bat --style=numbers --theme=$BAT_THEME --color=always {} || cat {}) 2>/dev/null | head -500'" 90 export FZF_DEFAULT_COMMAND="ag -l -g \"\"" 91 # script edit 92 se() { 93 local files 94 IFS=$'\n' files=($(command find "$DOTFILES/scripts" -type f -or -type l -not -ipath "*/.git/*" | fzf -m --inline-info --prompt="edit> " --height=40 --reverse)) 95 [[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}" 96 } 97 # script create 98 sc() { 99 [ -z "$1" ] && echo "Filename must be passed as an argument." && return 1 100 vim $DOTFILES/scripts/"$1" && chmod +x $DOTFILES/scripts/"$1" 101 } 102 103 # script find 104 sf() { 105 local f 106 IFS=$'\n' f=($(command find "$DOTFILES"/scripts -type f -not -ipath "*/.git/*" | fzf +m --prompt="find> " --height=40 --reverse --cycle)) 107 [[ -n "$f" ]] && printf "$f" 108 } 109 110 # config edit 111 ce() { 112 local files 113 IFS=$'\n' files=($(command find "$DOTFILES" -type f ! -ipath "*/scripts/*" ! -ipath "*/.git/*" ! -ipath "*/bin/*" ! -ipath "*/oh-my-zsh/*" ! -name ".DS_Store" | fzf -m --inline-info --prompt="edit> " --height=40 --reverse)) 114 [[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}" 115 } 116 117 # config find 118 cf() { 119 local f 120 IFS=$'\n' f=($(command find "$DOTFILES" -type f ! -ipath "*/.git/*" ! -ipath "*/bin/*" ! -ipath "*/oh-my-zsh/*" ! -name ".DS_Store" | fzf +m --prompt="find> " --height=40 --reverse --cycle)) 121 [[ -n "$f" ]] && printf "$f" 122 } 123 124 # vim setting edit 125 ve() { 126 local files 127 IFS=$'\n' files=($(command find ~/.dotfiles/vim -type f -not -ipath "*/.git/*" | fzf -m --inline-info --prompt="edit (vim)> " --height=40 --reverse)) 128 [[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}" 129 } 130 # run python 131 rp() { 132 fzf --print-query --preview-window=right:50% --preview 'python -c {q}' <<<'' 133 } 134 # Run ruby 135 rr() { 136 fzf --print-query --preview-window=right:50% --preview 'ruby -e {q}' <<<'' 137 } 138 # run bash 139 rb() { 140 fzf --print-query --preview-window=right:50% --preview 'bash -c {q}' <<<'' 141 } 142 # run json 143 rj() { 144 fzf --preview "cat $1 | jq {q}" 145 } 146 147 # list man pages 148 mans() { 149 sec="${1:-1|4}" 150 # (sorry, couldn't be arsed to deal with escaping parentheses for variable passing in awk) 151 manlist=$(man -k . 2>/dev/null | awk "$(printf 'BEGIN {FS=OFS="- "} /\([%s]\)/ {gsub(/\([0-9]\)/, "", $1); if (!seen[$0]++) { print }}' "$sec")" | fzf) \ 152 && man "$(echo "$manlist" | awk -F' |,' '{print $1}')" \ 153 && unset manlist 154 } 155 156 # list env variables 157 envs() { 158 var=$(printenv | cut -d= -f1 | fzf) \ 159 && echo "$var=$(printenv "$var")" \ 160 && unset var 161 } 162 fi 163 164 # lf-reliant functions 165 if command -v lf >/dev/null 2>&1; then 166 lfcd() { 167 tmp="$(mktemp)" 168 lf -last-dir-path="$tmp" "$@" 169 if [ -f "$tmp" ]; then 170 dir="$(cat "$tmp")" 171 rm "$tmp" 172 [ -d "$dir" ] && [ "$dir" != "$(pwd)" ] && cd "$dir" || return 173 fi 174 } 175 fi