Run Command filtered by egrep by fzf
Function
# This function will filter a list of commands by a given egrep filter
# then filter by fzf and run the selected command with the arguments that you provide.
#
# current limitation: does NOT work with quoted arguments.
#
# Example usage:
#
# # Setup some prefix for your run commands
# export YOUR_RUN_CMD_PREFIX="s2"
#
# # Now setup your aliases to create filtered command searches
# alias ${YOUR_RUN_CMD_PREFIX:?}git='command.filter_by_egrep_filter_by_fzf.then_run "^git|^_git"'
# alias ${YOUR_RUN_CMD_PREFIX:?}g='command.filter_by_egrep_filter_by_fzf.then_run "^git|^_git"'
command.filter_by_egrep_filter_by_fzf.then_run(){
local egrepFilter="${1:?filter to feed into grep -E}"
# Shift the egrep filter argument off the stack to only leave the optional arguments
# that we want to pass to subsequent commands.
shift
# Add the filtering command to history
history -s "command.filter_by_egrep_filter_by_fzf.then_run ${egrepFilter:?} ${*}";
local command
command=$(compgen -ac | grep -E "${egrepFilter:?}" | sort | uniq | fzf --tiebreak=length --prompt="Choose command that will run with arguments=[${*}]:")
# Add the command that you have chosen with arguments to history
history -s "${command:?} ${*:?}";
${command:?} "${@:?}"
}
export -f command.filter_by_egrep_filter_by_fzf.then_run
Example alias setup with this function:
# Setup some prefix for your run commands
export YOUR_RUN_CMD_PREFIX="s2"
# Now setup your aliases to create filtered command searches
# shellcheck disable=SC2139
alias ${YOUR_RUN_CMD_PREFIX:?}git='command.filter_by_egrep_filter_by_fzf.then_run "^git|^_git"'
# shellcheck disable=SC2139
alias ${YOUR_RUN_CMD_PREFIX:?}g='command.filter_by_egrep_filter_by_fzf.then_run "^git|^_git"'