nnn/plugins/fzhist

39 lines
845 B
Text
Raw Normal View History

2019-11-17 19:14:30 +05:30
#!/usr/bin/env sh
# Description: Fuzzy find a command from history, edit in $EDITOR and run as a command
# Currently supports only bash and fish history
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana
2019-12-09 18:36:48 +05:30
if which fzf >/dev/null 2>&1; then
fuzzy=fzf
else
exit 1
fi
2019-11-17 19:14:30 +05:30
shellname="$(basename "$SHELL")"
if [ "$shellname" = "bash" ]; then
hist_file="$HOME/.bash_history"
2019-12-09 18:36:48 +05:30
entry="$("$fuzzy" < "$hist_file")"
2019-11-17 19:14:30 +05:30
elif [ "$shellname" = "fish" ]; then
hist_file="$HOME/.config/fish/fish_history"
2019-12-09 18:36:48 +05:30
entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")"
2019-11-17 19:14:30 +05:30
fi
if ! [ -z "$entry" ]; then
tmpfile=$(mktemp)
2019-11-22 02:14:25 +05:30
echo "$entry" >> "$tmpfile"
$EDITOR "$tmpfile"
2019-11-17 19:14:30 +05:30
2019-11-22 02:14:25 +05:30
if [ -s "$tmpfile" ]; then
$SHELL -c "$(cat "$tmpfile")"
2019-11-17 19:14:30 +05:30
fi
2019-11-22 02:14:25 +05:30
rm "$tmpfile"
2019-11-17 21:56:05 +05:30
2019-11-22 02:14:25 +05:30
printf "Press any key to exit"
read -r _
2019-11-17 19:14:30 +05:30
fi