nnn/plugins/fzhist

33 lines
758 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
shellname="$(basename "$SHELL")"
if [ "$shellname" = "bash" ]; then
hist_file="$HOME/.bash_history"
2019-11-20 19:01:39 +00:00
entry="$(fzy < "$hist_file")"
2019-11-17 13:44:30 +00:00
elif [ "$shellname" = "fish" ]; then
hist_file="$HOME/.config/fish/fish_history"
2019-11-20 19:01:39 +00:00
entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | fzy)"
2019-11-17 13:44:30 +00:00
fi
if ! [ -z "$entry" ]; then
tmpfile=$(mktemp)
echo "$entry" >> $tmpfile
$EDITOR $tmpfile
2019-11-20 19:01:39 +00:00
if [ -s $tmpfile ]; then
$SHELL -c "$(cat $tmpfile)"
2019-11-17 13:44:30 +00:00
fi
rm $tmpfile
2019-11-17 16:26:05 +00:00
echo -n "Press any key to exit"
read input
2019-11-17 13:44:30 +00:00
fi