aboutsummaryrefslogtreecommitdiff
path: root/files/.config/nnn/plugins/fzhist
diff options
context:
space:
mode:
Diffstat (limited to 'files/.config/nnn/plugins/fzhist')
-rwxr-xr-xfiles/.config/nnn/plugins/fzhist40
1 files changed, 40 insertions, 0 deletions
diff --git a/files/.config/nnn/plugins/fzhist b/files/.config/nnn/plugins/fzhist
new file mode 100755
index 0000000..111bc22
--- /dev/null
+++ b/files/.config/nnn/plugins/fzhist
@@ -0,0 +1,40 @@
+#!/usr/bin/env sh
+
+# Description: Fuzzy find a command from history,
+# edit in $EDITOR and run as a command
+#
+# Note: Supports only bash and fish history
+#
+# Shell: POSIX compliant
+# Author: Arun Prakash Jana
+
+if type fzf >/dev/null 2>&1; then
+ fuzzy=fzf
+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/.local/share/fish/fish_history"
+ entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")"
+fi
+
+if [ -n "$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