Skip to content

Instantly share code, notes, and snippets.

@m1st0
Last active June 27, 2024 07:47
Show Gist options
  • Save m1st0/70e25d3f265e9fcec4c6c36a4e650fdb to your computer and use it in GitHub Desktop.
Save m1st0/70e25d3f265e9fcec4c6c36a4e650fdb to your computer and use it in GitHub Desktop.
# Setup fzf in Ubuntu from 24.04 and above.
# Soft link to the full path of this script
# from ".fzf.bash" using "ln -s"
# Source the softlink ".fzf.bash" in your ".bashrc"
#
# From https://www.josean.com/posts/7-amazing-cli-tools
#
# Copyright (C) 2024 Josean Martinez
# https://github.com/josean-dev
# https://www.youtube.com/channel/UC_NZ6qLS9oJgsMKQhqAkg-w
#
# Copyright (C) 2024 Maulik Mistry <mistry01@gmail.com>
# https://github.com/m1st0
# From Ubuntu 24.04, "fd, bat, exa" have differing package
# names or versions
# To install them, use "sudo apt install fdfind batcat eza"
# Provide location of fzf bin directory if you get a newer
# version than Ubuntu
fzf_path="$HOME/my_builds/fzf/bin"
if [[ ! "$PATH" == *$fzf_path* ]]; then
PATH="${PATH:+${PATH}:}$fzf_path"
fi
eval "$(fzf --bash)"
# Use fdfind instead of find
export FZF_DEFAULT_COMMAND="fdfind --hidden --strip-cwd-prefix --color=always --exclude .git"
export FZF_CTRL_T_COMMAND="${FZF_DEFAULT_COMMAND}"
export FZF_ALT_C_COMMAND="fdfind --type=d --hidden --strip-cwd-prefix --color=always --exclude .git"
# The first argument to the function ($1) is the base path to start traversal
# See the source code (completion.{bash,zsh}) for the details
_fzf_compgen_path() {
fdfind --hidden --color=always --exclude .git . "$1"
}
_fzf_compgen_dir() {
fdfind --type=d --hidden --color=always --exclude .git . "$1"
}
# Determine how to preview directories vs files
# To preserve colors, "--ansi" is used
show_file_or_dir_preview="if [ -d {} ]; then eza --tree {} --color=always | head -200; else batcat -n --color=always --line-range :500 {}; fi"
export FZF_CTRL_T_OPTS="--ansi --preview '$show_file_or_dir_preview'"
export FZF_ALT_C_OPTS="--ansi --preview 'eza --tree {} --color=always | head -200'"
# Advanced customization of fzf options via _fzf_comprun function
# The first argument to the function is the name of the command
# You should make sure to pass the rest of the arguments to fzf
# To preserve colors "--ansi" is used
_fzf_comprun() {
local command=$1
shift
case "$command" in
cd) fzf --ansi --preview 'eza --tree {} --color=always | head -200' "$@" ;;
export|unset) fzf --ansi --preview "eval 'echo ${}'" "$@" ;;
ssh) fzf --ansi --preview 'dig {}' "$@" ;;
*) fzf --ansi --preview "$show_file_or_dir_preview" "$@" ;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment