Search code examples
grepgit-bash

How to create an "alias" that search for an "alias"


I need to create an alias that receive a parameter, then search for all alias that has that parameter in this way: alias+space+parameter+"other text"

My aliases:

alias att='alias | grep --color  $"alias".$1'
alias ass='alias | grep --color "alias "$1'
alias as='alias | grep --color $1'
alias assu='alias | grep -i --color="always"  "^alias" | grep --color="always" $1'
alias ffindd='ls ~/ | grep --color=always $1'
alias ffnexn='find . -type d -name node_modules -prune -o -type f -name $1 -print' 
alias ffnexx='find . -type d -name $1 -prune -o -type f -name $2 -print'
alias ffcexn='find . -type d -name node_modules -prune -o -type f -print | xargs grep --color=always $1'
alias ffctc='find . -type f -name $2 | xargs grep --color=always $1'

My search: All aliases that named "as"

What I try:

att "as" 

What I expecting:

alias ass='alias | grep --color "alias "$1'
alias as='alias | grep --color $1'

What I received:

grep: as: No such file or directory

I also tried:

assu "as"

What I expecting:

alias ass='alias | grep --color "alias "$1'
alias as='alias | grep --color $1'

What I received:

all my alias that has "alias" and "as"


Solution

  • I need to create an alias that receive a parameter

    That is what functions are for. Aliases are very simple. If a command line starts with a token that can be found in the list of aliases then the token is swapped for the alias text. The new resulting command line is then evaluated. This forces aliases to pass all options to the underlying executable verbatim. You cannot change argument order or munge with other arguments. Functions give you much more power for just a little more syntax.

    When you enter att "as" at the command line, the shell turns it into:

    alias | grep --color  $"alias".$1' "as"
    

    As "as" is the second positional argument to grep, grep considers it a file it needs to search. And so fails when it cannot find a file called as.

    A function is more or less the same, but does exactly what you want. And you call it exactly how you were calling att before.

    function att() {
        alias | grep --color "alias.$1"
    }
    att "as"
    

    prints:

    alias as='alias | grep --color $1'
    

    Remember to unset any alias with the same name as the function before calling the new function (or use a new shell). Aliases have a higher precedence than both executables and functions. So if you have an alias with the same name as a function, then the alias will hide the function.

    If you're now using functions, then you might want a function that shows you the definition of the function. That can be done simply as type <function-name> eg. type acc. Which would print:

    att is a function
    att ()
    {
        alias | grep --color=auto "alias.$1"
    }
    

    Actually, I suppose a full replacement that searches for a prefix and shows you the definition would be:

    function find_functions() {
        if [[ $# != 1 ]]; then
            echo "find_functions must be called with exactly one arg" >&2
            return 1
        fi
        funcs=( $(declare -F | grep "^declare -f $1" | cut -d' ' -f3 ) )
        type "${funcs[@]}"
    }