nnn/plugins/fzhist

54 lines
1.2 KiB
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, zsh and fish history
#
# TODO: For zsh there's also $ZDOTDIR which might need to be checked as well for the -z $HISTFILE && -n $ZDOTDIR case.
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
if [ -f "$HISTFILE" ]; then
hist_file="$HISTFILE"
else
hist_file="$HOME/.bash_history"
fi
entry="$("$fuzzy" < "$hist_file")"
elif [ "$shellname" = "zsh" ]; then
if [ -f "$HISTFILE" ]; then
hist_file="$HISTFILE"
else
hist_file="$HOME/.zsh_history"
fi
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/.local/share/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