fzhist (847B)
1 #!/usr/bin/env sh 2 3 # Description: Fuzzy find a command from history, 4 # edit in $EDITOR and run as a command 5 # 6 # Note: Supports only bash and fish history 7 # 8 # Shell: POSIX compliant 9 # Author: Arun Prakash Jana 10 11 if type fzf >/dev/null 2>&1; then 12 fuzzy=fzf 13 else 14 exit 1 15 fi 16 17 shellname="$(basename "$SHELL")" 18 19 if [ "$shellname" = "bash" ]; then 20 hist_file="$HOME/.bash_history" 21 entry="$("$fuzzy" < "$hist_file")" 22 elif [ "$shellname" = "fish" ]; then 23 hist_file="$HOME/.local/share/fish/fish_history" 24 entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")" 25 fi 26 27 if [ -n "$entry" ]; then 28 tmpfile=$(mktemp) 29 echo "$entry" >> "$tmpfile" 30 $EDITOR "$tmpfile" 31 32 if [ -s "$tmpfile" ]; then 33 $SHELL -c "$(cat "$tmpfile")" 34 fi 35 36 rm "$tmpfile" 37 38 printf "Press any key to exit" 39 read -r _ 40 fi