nnn/plugins/fzhist

39 lines
845 B
Plaintext
Raw Normal View History

2019-11-17 13:44:30 +00:00
#!/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 13:06:48 +00:00
if which fzf >/dev/null 2>&1; then
fuzzy=fzf
else
exit 1
fi
2019-11-17 13:44:30 +00:00
shellname="$(basename "$SHELL")"
if [ "$shellname" = "bash" ]; then
hist_file="$HOME/.bash_history"
2019-12-09 13:06:48 +00:00
entry="$("$fuzzy" < "$hist_file")"
2019-11-17 13:44:30 +00:00
elif [ "$shellname" = "fish" ]; then
hist_file="$HOME/.config/fish/fish_history"
2019-12-09 13:06:48 +00:00
entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")"
2019-11-17 13:44:30 +00:00
fi
if ! [ -z "$entry" ]; then
tmpfile=$(mktemp)
2019-11-21 20:44:25 +00:00
echo "$entry" >> "$tmpfile"
$EDITOR "$tmpfile"
2019-11-17 13:44:30 +00:00
2019-11-21 20:44:25 +00:00
if [ -s "$tmpfile" ]; then
$SHELL -c "$(cat "$tmpfile")"
2019-11-17 13:44:30 +00:00
fi
2019-11-21 20:44:25 +00:00
rm "$tmpfile"
2019-11-17 16:26:05 +00:00
2019-11-21 20:44:25 +00:00
printf "Press any key to exit"
read -r _
2019-11-17 13:44:30 +00:00
fi