Skip to content

Instantly share code, notes, and snippets.

@brbsix
Last active July 9, 2023 01:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brbsix/5e4f18833133b3accac78e333129295b to your computer and use it in GitHub Desktop.
Save brbsix/5e4f18833133b3accac78e333129295b to your computer and use it in GitHub Desktop.
Configure fzf
#!/bin/bash
#
# Configure fzf
# Set up fzf tab completion
. /usr/share/fzf/completion.bash
# Set up fzf key bindings
. /usr/share/fzf/key-bindings.bash
# Unset vars to prevent them from being appended to multiple times if bash
# shells are nested and as a result .bashrc is sourced multiple times
unset FZF_ALT_C_OPTS FZF_CTRL_R_OPTS FZF_DEFAULT_OPTS
# View full path in preview window (?)
export FZF_ALT_C_OPTS="${FZF_ALT_C_OPTS:+$FZF_ALT_C_OPTS }--preview 'echo {}' --preview-window down:5:hidden:wrap --bind '?:toggle-preview'"
# View full command in preview window (?)
export FZF_CTRL_R_OPTS="${FZF_CTRL_R_OPTS:+$FZF_CTRL_R_OPTS }--preview 'echo {}' --preview-window down:5:hidden:wrap --bind '?:toggle-preview'"
# Exact-match rather than fuzzy matching by default (use ' to negate)
export FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS:+$FZF_DEFAULT_OPTS }--exact"
# Prevent fzf from reducing height to 40% by default
export FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS:+$FZF_DEFAULT_OPTS }--no-height"
# Directly execute the command (Ctrl-X Ctrl-R)
bind "$(bind -s | grep '^"\\C-r"' | sed 's/"/"\\C-x/;s/"$/\\C-m"/')"
@tiwaria1
Copy link

This was very helpful thank you! Specially the Ctrl+R binding to highlight the full wrapped line of a command in history search. :)

@tiwaria1
Copy link

I applied this same idea to the ALT_C binding:

export FZF_ALT_C_OPTS="${FZF_ALT_C_OPTS:+$FZF_ALT_C_OPTS }--preview 'echo {}' --preview-window down:5:hidden:wrap --bind '?:toggle-preview'"

@brbsix
Copy link
Author

brbsix commented Mar 11, 2022

@tiwaria1 Great suggestion, glad you found it helpful. I updated the gist.

@cohml
Copy link

cohml commented Jul 9, 2023

This was very helpful thank you! Specially the Ctrl+R binding to highlight the full wrapped line of a command in history search. :)

Agree. This is gold. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment