Ctrl-R Replacement with FZF for Bash

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.

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.

:sparkles: 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.

:hammer_and_wrench: Requirements

  • Bash
  • fzf installed and available in your PATH.

:package: Installation

  1. Make sure fzf is installed.
  2. Add the following function to your ~/.bashrc:
__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:
source ~/.bashrc

:computer_mouse: 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.

:brain: Note

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

:memo: 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…
:people_hugging: :argentina:

1 Like

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

Nunca lo ví…
I never saw it…

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):

th

1 Like

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?

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

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

Usage:

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

3 Likes

De hecho, hay muchas interesantes fucniones de Mabox que se descubren y revelan en el Manual de Mabox y en la Guía del Usuario de Mabox (son dos diferentes). Contienen muchos detallitos de este tipo, tipo joyas de Mabox

1 Like

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…)

2 Likes