A Bash function for finding your Bash functions
As you may have noticed if you’ve followed this blog lately, I write a lot of Bash functions and aliases. I keep them in distinct files using a plugin system based on bash-it. It works really well, and makes upkeep simple. However, I very often lose track of exactly where I defined a function or alias after a few months go by.
Unix commands like
which will tell you where executable scripts and binaries are, but they’ll fail finding a function. Thus, “where” was born. It’s a Bash function that indexes every function and alias in any files that are sourced during login. Then you can type
where func_name and find out exactly where a function or alias was defined. If a function/alias appears multiple times in your sourced files, the last version sourced (and thus likely the one Bash will default to) will win.
where has options for degrees of fuzzy search, too. You can use
-k (aliased to
where?) to find any functions containing the search string. There’s
where*) to do a completely fuzzy search of the index as well.
The search will fall back to using
type if there are no results in its index, so it becomes a universal command for me, replacing
type for the most part.
The basic command outputs a full path (with :lineno) if a match is found. In iTerm, you can just ⌘-click that to edit the file. You can also use a
-E flag to immediately open a match in your
The fully-automatic version hooks the builtin
source command. (There are included routines for taking a more manual approach as well.) Because hooking the
source command and parsing every file you load can be slow, the index that
where creates can be cached with any expiration time you want. See the project page for more info.