Skip to content

Instantly share code, notes, and snippets.

@BorisAnthony
Last active May 24, 2026 15:34
Show Gist options
  • Select an option

  • Save BorisAnthony/3bd35ee39b61a8333b56cfe07d78191c to your computer and use it in GitHub Desktop.

Select an option

Save BorisAnthony/3bd35ee39b61a8333b56cfe07d78191c to your computer and use it in GitHub Desktop.
Fuzzy-search for launching a command, or listing its source.
# Fuzzy Command Search:
# Fuzzy-search for a command, to run it or get info about it
# ---
# Add to your .zshrc or whatever
# Requirements:
# - `fzf` be installed
# - `ZSH_CONFIG_DIRS` var be set
# e.g.: export ZSH_CONFIG_DIRS="${HOME}/.zsh_configs/"
# ---
# Usage:
# To get command to run it:
# - `cc` or `cc <search string>` to pre-populate the search query.
# To get information about a command:
# - `lc` or `lc <search string>` to pre-populate the search query.
# ---
# https://gist.github.com/BorisAnthony/3bd35ee39b61a8333b56cfe07d78191c
# ---
command_search_fzf() {
command -v fzf >/dev/null || return 1
local selected
selected=$(
print -rl -- \
${(k)aliases} \
${(k)functions} \
${(k)builtins} \
${(k)commands} \
| grep -vE '^[.+:_\[\-]' \
| sort -u \
| fzf --query="$*" --reverse
)
[[ -n $selected ]] && print -- "$selected"
return 0
}
# COMMAND SEARCH & EXECUTE
# Pushes selected command into the ZLE buffer for editing
cc() {
local selected
selected=$(command_search_fzf "$@")
[[ -n $selected ]] && print -z "$selected"
return 0
}
# COMMAND SEARCH & LIST
# Lists a command's type and location
lc() {
local cmd
cmd=$(command_search_fzf "$@")
[[ -z $cmd ]] && return 0
local cmd_type
cmd_type=$(whence -w "$cmd") # e.g. "git: command", "ll: alias", "precmd: function"
print "" # newline for spacing
case "$cmd_type" in
*': command')
local cmd_path
cmd_path=$(command -v "$cmd")
print "\e[1;32m${cmd}\e[0m"
print "— \e[0;32m\e[3mcommand\e[0m → \e[0;32m${cmd_path}\e[0m"
ll "$cmd_path"
;;
*': alias')
print "\e[1;96m${cmd}\e[0m"
print "— \e[0;96m\e[3malias\e[0m → \e[96m$(whence "$cmd")\e[0m"
;;
*': function')
print "\e[1;34m${cmd}\e[0m"
print "— \e[0;34m\e[3mfunction\e[0m ↓\e[0;94m"
whence -fv "$cmd"
local src
# src=$(grep -rl "^${cmd}[[:space:]]*(" ~/.DOTFILES/.zsh_configs/ 2>/dev/null | head -1)
src=$(grep -rl --fixed-strings "${cmd}(" "$ZSH_CONFIG_DIRS" 2>/dev/null | head -1)
[[ -n $src ]] && print "— \e[34m\e[3mdefined in\e[0m → \e[34m${src}\e[0m"
;;
*': builtin')
print "\e[1;93m${cmd}\e[0m"
print "— \e[0;93m\e[3mbuiltin\e[0m → \e[93mshell\e[0m"
;;
*)
print "\e[31mlc: $cmd: not found\e[0m" >&2
return 1
;;
esac
print "" # newline for spacing
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment