.fzf_helpers (3845B)
1 2 fze() { 3 local file 4 file=$(git ls-files | fzf) && edit "$file" 5 } 6 7 # ctags jumper 8 fte() { 9 <tags sed -En '/^!/d;s,^([^\t]+).*,\1,p' 10 ctags_list | fzf 11 } 12 13 # fd - cd to selected directory 14 fd() { 15 local dir 16 dir=$(find ${1:-.} -path '*/\.*' -prune \ 17 -o -type d -print 2> /dev/null | fzf +m) && 18 cd "$dir" 19 } 20 21 nma() { 22 if [ ! -e ~/.notmuch-address-cache ]; then 23 (notmuch-personal address \*; notmuch address \*) | sort -u > ~/.notmuch-address-cache 24 fi 25 fzy < ~/.notmuch-address-cache | tee /dev/tty | xclip 26 } 27 28 # fda - including hidden directories 29 fda() { 30 local dir 31 dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir" 32 } 33 34 flp () { 35 id="$(lpass ls | fzf | grep -P '\d+' -o)" 36 [ ! -z "$id" ] && lpass show "$id" 37 } 38 39 # fz - cd to directories known to z 40 fz() { 41 local dir=$(z | fzcmd "$1") && cd "$dir" 42 } 43 44 findlt () { 45 local all=$(find . -maxdepth "$1" -type "${2:-f}" -printf "%T@ %Tc %p\n" | sort -nr | cut -f2- -d" " | fzf +s) 46 <<<"$all" cut -f3- -d" " 47 } 48 49 ftd() { 50 local res="$(findlt ${1:-1} d)" 51 cd "$res" 52 } 53 54 ctags_list() { 55 <tags sed -En '/^!/d;s,^([^\t]+).*,\1,p' 56 } 57 58 # ctags less jumper 59 fct() { 60 ctags_list | fzf | xargs -I{} less -t{} 61 } 62 63 ft() { 64 local res=$(findlt ${1:-1} f) 65 <<<$res xclip 66 printf "%s\n" "$res" 67 } 68 69 fto() { 70 open "$(findlt ${1:-1} f)" 71 } 72 73 # fzt - cd to most recently accessed dirs 74 fzt() { 75 local dir=$(z -et | fzcmd) && cd "$dir" 76 } 77 78 # fz helper 79 fzcmd() { 80 awk '{ print $2 }' | fzf --query="$1" --tac +s 81 } 82 83 # fn - nix packages 84 fn() { 85 fzf < ~/.nix-grep-cache 86 } 87 88 # fni - nix install 89 fni() { 90 local pkg 91 pkg=$(<$HOME/.nix-grep-cache fzf) && \ 92 nix-install $(echo $pkg | awk '{ print $1 }') 93 } 94 95 # fns - nix shell 96 fns() { 97 local pkg 98 pkg=$(<$HOME/.nixenv.cache fzf) && \ 99 nix-shell -p $(echo $pkg | awk '{ print $1 }') 100 } 101 102 # fdr - cd to selected parent directory 103 fdr() { 104 local declare dirs=() 105 get_parent_dirs() { 106 if [[ -d "${1}" ]]; then dirs+=("$1"); else return; fi 107 if [[ "${1}" == '/' ]]; then 108 for _dir in "${dirs[@]}"; do echo $_dir; done 109 else 110 get_parent_dirs $(dirname "$1") 111 fi 112 } 113 local DIR=$(get_parent_dirs $(realpath "${1:-$(pwd)}") | fzf --tac) 114 cd "$DIR" 115 } 116 117 # cdf - cd into the directory of the selected file 118 cdf() { 119 local file 120 local dir 121 file=$(fzf +m -q "$1") && dir=$(dirname "$file") && cd "$dir" 122 } 123 124 # fh - repeat history, assumes zsh 125 fh() { 126 print -z $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed 's/ *[0-9]* *//') 127 } 128 129 fo() { 130 local out file key 131 out=$(fzf --query="$1" --exit-0 --expect=ctrl-o,ctrl-e) 132 key=$(head -1 <<< "$out") 133 file=$(head -2 <<< "$out" | tail -1) 134 if [ -n "$file" ]; then 135 [ "$key" = ctrl-o ] && open "$file" || edit "$file" 136 fi 137 } 138 139 # 140 # GIT 141 # 142 143 fshow() { 144 git log --graph --color=always \ 145 --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" | 146 fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \ 147 --bind "ctrl-m:execute: 148 (grep -o '[a-f0-9]\{7\}' | head -1 | 149 xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF' 150 {} 151 FZF-EOF" 152 } 153 154 fenv() { 155 local out 156 out=$(env | fzf) 157 echo $(echo $out | cut -d= -f2) 158 } 159 160 fstash() { 161 local out q k sha 162 while out=$( 163 git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" | 164 fzf --ansi --no-sort --query="$q" --print-query \ 165 --expect=ctrl-d,ctrl-b); 166 do 167 mapfile -t out <<< "$out" 168 q="${out[0]}" 169 k="${out[1]}" 170 sha="${out[-1]}" 171 sha="${sha%% *}" 172 [[ -z "$sha" ]] && continue 173 if [[ "$k" == 'ctrl-d' ]]; then 174 git diff $sha 175 elif [[ "$k" == 'ctrl-b' ]]; then 176 git stash branch "stash-$sha" $sha 177 break; 178 else 179 git stash show -p $sha 180 fi 181 done 182 }