# Ctrl-R Replacement with FZF for Bash

**URL:** https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285
**Category:** Userland
**Created:** [July 7, 2025, 5:01pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285 "2025-07-07T17:01:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![danielhb](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/danielhb/32/3941_2.png) [@danielhb](https://forum.maboxlinux.org/u/danielhb)
#### Post date: [July 7, 2025, 5:01pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/1 "2025-07-07T17:01:38Z")

</div>

Este script mejora la búsqueda en el historial de comandos en Bash, reemplazando la función estándar de Ctrl-R con una interfaz  
interactiva y moderna basada en fzf.

* * *

This script enhances Bash’s reverse history search by replacing the default Ctrl-R behavior with an interactive, modern  
interface powered by fzf.

 ![image](https://forum.maboxlinux.org/uploads/default/original/2X/7/751faaee319eff9ca75a3969cb1a99a6c90eadb7.png)

# Ctrl-R Replacement with FZF for Bash

This script enhances Bash’s reverse history search by replacing the default `Ctrl-R` behavior with an interactive, modern interface powered by `fzf`.

## ✨ Features

- Searches your Bash history in reverse order (most recent first).
- Interactive fuzzy search with `fzf`.
- Selected command is inserted directly into the prompt, **editable**.
- Works with Bash.
- Detects system language (Spanish or English) and adapts interface texts.
- Clean visual style with borders, colors, and margins.

## 🛠 Requirements

- Bash
- [`fzf`](https://github.com/junegunn/fzf) installed and available in your `PATH`.

## 📦 Installation

1. Make sure `fzf` is installed.
2. Add the following function to your `~/.bashrc`:

```bash
__fzf_historial() {
  echo " "
  echo -ne "\033[2;H"

  local histfile="${HISTFILE:-$HOME/.bash_history}"
  [[! -f "$histfile"]] && return

  local lang="${LANG:-en}"
  local titulo prompt
  if [["$lang" =~ ^es]]; then
    titulo="Historial de comandos"
    prompt="Comando ▶ "
  else
    titulo="Command history"
    prompt="Command ▶ "
  fi

  local cmd
  cmd=$(awk '{ l[NR]=$0 } END { for (i=NR;i>0;i--) print l[i] }' "$histfile" \
        | grep -v '^#' | grep -v '^$' \
        | fzf \
            --height=50% \
            --layout=default \
            --border=rounded \
            --cycle \
            --border-label="] $titulo [" \
            --preview='echo {}' \
            --preview-window=down:0:hidden \
            --margin=1,35% \
            --color='border:2,label:6:bold,bg+:1,fg+:15:bold,pointer:6:bold,marker:2:bold,prompt:6:bold' \
            --pointer='▶ ' \
            --marker='▶ ' \
            --prompt="$prompt")

  [[-n "$cmd"]] && READLINE_LINE="$cmd" && READLINE_POINT="${#cmd}"
}

bind -x '"\C-r": __fzf_historial'

```

1. Apply the changes with:

```bash
source ~/.bashrc

```

## 🖱 Usage

Press `Ctrl-R` in your Bash prompt.  
An interactive history menu will appear. Once you choose a command, it will be inserted directly into the prompt so you can edit or run it.

## 🧠 Note

This method **does not auto-execute** the selected command. It gives you the chance to review and modify it first.

## 📝 License

This script is free to use and modify. You may share it under the MIT license or similar.

* * *

Si a alguien le sirve…  
If it helps anyone…  
🫂 🇦🇷

---

<div class="post-metadata">

### Author: ![napcok](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/napcok/32/410_2.png) [@napcok](https://forum.maboxlinux.org/u/napcok)
#### Post date: [July 7, 2025, 5:22pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/2 "2025-07-07T17:22:39Z")

</div>

We have this preconfigured in Mabox by default… since many years now 😃

---

<div class="post-metadata">

### Author: ![danielhb](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/danielhb/32/3941_2.png) [@danielhb](https://forum.maboxlinux.org/u/danielhb)
#### Post date: [July 7, 2025, 5:24pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/3 "2025-07-07T17:24:35Z")

</div>

Nunca lo ví…  
I never saw it…

---

<div class="post-metadata">

### Author: ![napcok](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/napcok/32/410_2.png) [@napcok](https://forum.maboxlinux.org/u/napcok)
#### Post date: [July 7, 2025, 5:36pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/4 "2025-07-07T17:36:36Z")

</div>

Three fzf based utilities are preconfiguired and ready to use in Mabox:

`CTRL + r` fzf history  
`CTRL + t` dirname/filename picker  
`ALT + c` fzf cd

Edit:  
And also fzf based interactive terminal theme selector (with more than 400 themes available):

```auto
th

```

 ![Mabox_20250707-19-41-57](https://forum.maboxlinux.org/uploads/default/original/2X/8/8f0ec501fcf0805fa712068ece2307183e6d9b5d.png)

---

<div class="post-metadata">

### Author: ![danielhb](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/danielhb/32/3941_2.png) [@danielhb](https://forum.maboxlinux.org/u/danielhb)
#### Post date: [July 7, 2025, 5:41pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/5 "2025-07-07T17:41:47Z")

</div>

They always worked for me as defaults in bash… could it be that oh my bash overwrote them?

Siempre me funcionaron como predeterminadas por bash… ¿puede ser que oh my bash las haya sobreescrito?

---

<div class="post-metadata">

### Author: ![napcok](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/napcok/32/410_2.png) [@napcok](https://forum.maboxlinux.org/u/napcok)
#### Post date: [July 7, 2025, 5:44pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/6 "2025-07-07T17:44:37Z")

</div>

I don’t know, I never used oh my bash.

---

<div class="post-metadata">

### Author: ![napcok](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/napcok/32/410_2.png) [@napcok](https://forum.maboxlinux.org/u/napcok)
#### Post date: [July 13, 2025, 10:18am UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/7 "2025-07-13T10:18:49Z")

</div>

I remembered that Mabox has another fzf-based script installed by default - **fuzzy-pkg-finder**

> **[GitHub - ericlay/fuzzy-pkg-finder: Simple cli utility for using fzf to search...](https://github.com/ericlay/fuzzy-pkg-finder)**
>
> Simple cli utility for using fzf to search for/list/install/remove packages.

### Usage:

```auto
Syntax: fpf [-a| --aur] [-l| --list-installed] [-la| --list-aur-installed]
              [R| --remove] [-o| --orphans] [-U | --update] [-h | --help]
Defaults to Pacman if no options passed

Searching for a package:
ex: fpf [pkg name] for official repo search
ex: fpf -a [pkg name] for aur search

Options:
-a, --aur
    Search/List and install from AUR with Yay

-l, --list-installed
    Search/List installed packages from official repo

-la, --list-aur-installed
    Search/List installed packages from AUR 

-R, -remove
     Search/List installed packages for removal

-o, --orphans
     Search/List orphaned packages for removal

-U, --update
     Shows packages with updates available

-h, --help
     Print this help screen

```

### Keybinds:

`ctrl + /` Toggle the preview window  
`ctrl + h` Show help in the preview window  
`ctrl + k` Show the keybinds in the preview window  
`ctrl + n` Move to the next selected item  
`ctrl + b` Back to previoius selected item

When browsing AUR or installed Aur pkgs:  
`ctrl + p` Preview the highlighted pkgbuild file  
`ctrl + x` Return to the highlighted pkg info

---

<div class="post-metadata">

### Author: ![lobaluna](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/lobaluna/32/640_2.png) [@lobaluna](https://forum.maboxlinux.org/u/lobaluna)
#### Post date: [July 13, 2025, 7:26pm UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/8 "2025-07-13T19:26:22Z")

</div>

De hecho, hay muchas interesantes fucniones de Mabox que se descubren y revelan en el [Manual de Mabox](https://manual.maboxlinux.org/en/) y en la [Guía del Usuario de Mabox](https://maboxlinux.org/users-guide/) (son dos diferentes). Contienen muchos detallitos de este tipo, tipo joyas de Mabox

---

<div class="post-metadata">

### Author: ![danielhb](https://forum.maboxlinux.org/user_avatar/forum.maboxlinux.org/danielhb/32/3941_2.png) [@danielhb](https://forum.maboxlinux.org/u/danielhb)
#### Post date: [July 14, 2025, 12:58am UTC](https://forum.maboxlinux.org/t/ctrl-r-replacement-with-fzf-for-bash/2285/9 "2025-07-14T00:58:49Z")

</div>

FZF es una joya, lo uso mucho con pequeños scripts para uso personal (estoy aprendiendo…)

* * *

FZF is a gem, I use it a lot with small scripts for personal use (I’m just learning…)
