nnn/plugins/fzhist

41 lines
842 B
Plaintext
Raw Normal View History

2019-11-17 13:44:30 +00:00
#!/usr/bin/env sh
2021-05-15 17:32:01 +00:00
# Description: Fuzzy find a command from history,
# edit in $EDITOR and run as a command
#
# Note: Supports only bash and fish history
2019-11-17 13:44:30 +00:00
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana
if type fzf >/dev/null 2>&1; then
2019-12-09 13:06:48 +00:00
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
2020-11-22 14:39:14 +00:00
if [ -n "$entry" ]; then
2019-11-17 13:44:30 +00:00
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