fzhist (1250B)
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, zsh and fish history 7 # 8 # TODO: For zsh there's also $ZDOTDIR which might need to be checked as well for the -z $HISTFILE && -n $ZDOTDIR case. 9 # 10 # Shell: POSIX compliant 11 # Author: Arun Prakash Jana 12 13 if type fzf >/dev/null 2>&1; then 14 fuzzy=fzf 15 else 16 exit 1 17 fi 18 19 shellname="$(basename "$SHELL")" 20 21 if [ "$shellname" = "bash" ]; then 22 if [ -f "$HISTFILE" ]; then 23 hist_file="$HISTFILE" 24 else 25 hist_file="$HOME/.bash_history" 26 fi 27 entry="$("$fuzzy" < "$hist_file")" 28 elif [ "$shellname" = "zsh" ]; then 29 if [ -f "$HISTFILE" ]; then 30 hist_file="$HISTFILE" 31 else 32 hist_file="$HOME/.zsh_history" 33 fi 34 entry="$("$fuzzy" < "$hist_file")" 35 elif [ "$shellname" = "fish" ]; then 36 hist_file="$HOME/.local/share/fish/fish_history" 37 entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")" 38 fi 39 40 if [ -n "$entry" ]; then 41 tmpfile=$(mktemp) 42 echo "$entry" >> "$tmpfile" 43 $EDITOR "$tmpfile" 44 45 if [ -s "$tmpfile" ]; then 46 $SHELL -c "$(cat "$tmpfile")" 47 fi 48 49 rm -- "$tmpfile" 50 51 printf "Press any key to exit" 52 read -r _ 53 fi