mirror of
https://github.com/jarun/nnn.git
synced 2024-11-16 16:13:16 +00:00
41 lines
896 B
Bash
Executable file
41 lines
896 B
Bash
Executable file
#!/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
|
|
|
|
if which fzf >/dev/null 2>&1; then
|
|
fuzzy=fzf
|
|
elif which fzy >/dev/null 2>&1; then
|
|
fuzzy=fzy
|
|
else
|
|
exit 1
|
|
fi
|
|
|
|
shellname="$(basename "$SHELL")"
|
|
|
|
if [ "$shellname" = "bash" ]; then
|
|
hist_file="$HOME/.bash_history"
|
|
entry="$("$fuzzy" < "$hist_file")"
|
|
elif [ "$shellname" = "fish" ]; then
|
|
hist_file="$HOME/.config/fish/fish_history"
|
|
entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")"
|
|
fi
|
|
|
|
if ! [ -z "$entry" ]; then
|
|
tmpfile=$(mktemp)
|
|
echo "$entry" >> "$tmpfile"
|
|
$EDITOR "$tmpfile"
|
|
|
|
if [ -s "$tmpfile" ]; then
|
|
$SHELL -c "$(cat "$tmpfile")"
|
|
fi
|
|
|
|
rm "$tmpfile"
|
|
|
|
printf "Press any key to exit"
|
|
read -r _
|
|
fi
|