From 45dee6774c00d1315274bdc3b31a4ebdb9dd295b Mon Sep 17 00:00:00 2001
From: "Kenneth Benzie (Benie)" <benie@infektor.net>
Date: Sun, 7 Apr 2024 19:27:48 +0100
Subject: [PATCH] Add fuzzy history search with fzf

---
 zshrc | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/zshrc b/zshrc
index 2aae63b..27bc5c4 100644
--- a/zshrc
+++ b/zshrc
@@ -142,6 +142,18 @@ if [[ `uname` = Linux ]]; then
   [[ -n ${key[End]} ]] && bindkey "${key[End]}" end-of-line
 fi
 
+# Fuzzy history search with fzf
+function fzf-history-search() {
+  local selected
+  selected=$(history | sed 's/ *[0-9]* *//' | fzf --tac +s +m -1)
+  if [[ -n "$selected" ]]; then
+    BUFFER="${selected}"
+    zle end-of-line
+  fi
+}
+zle -N fzf-history-search
+bindkey '^R' fzf-history-search
+
 # Enable changing cursor shape based on vi mode
 if [ "$ITERM_PROFILE" != "" ] && [ "$TMUX" = "" ]; then
   # iTerm2 cursor shape escape sequences outside tmux