4

To improve on the steps I have to take to complete some actions on my Mac, I have created many aliases. Example:

  1. To open System Preferences, which I have to do often to manage multiple monitors, I have created the following alias in ~/.bash_profile:

    alias pref='open -a System\ Preferences'
    
  2. To put my setup in "Work From Home" mode, I have:

    alias wfh='code&rdc&smc&'
    

    which opens Visual Studio Code, the Remote Desktop and the Cisco Mobility Client.

  3. To work with aliases themselves I have:

    alias editbash='nano ~/.bash_profile;source ~/.bash_profile'
    

    which opens the ~/.bash_profile in nano editor and then when I'm done, it automatically sources the updated contents of the file for me.


Question:

I have numerous aliases that are all related to git. I often forget the alias for some actions. However, the good news is that all of my git aliases start with gt.

How can I list only the aliases that start with a particular string?

4

grep for them


  1. Far better suggestion from the comments - grep on alias.

    You can list all your aliases, even the ones not written in ~/.bash_profile, by calling alias.

    grep the result to find the aliases starting from gt as:

    alias | grep "^alias gt"
    

  1. Since the aliases are created by writing alias <alias_name>=..., to list the aliases starting with, for example, gt, you can do:

    grep "^alias gt" ~/.bash_profile
    

    The ^ in the grep argument is an anchor. The caret ^ and the dollar sign $ are meta-characters that respectively match the empty string at the beginning and end of a line.

    In ^alias gt, it implies that you want only those lines that start with "alias gt". On my machine, I get the following result:

    alias gts="git status" 
    alias gtd="git diff" 
    alias gtpull="git pull"
    alias gtb="git branch"  
    alias gtpush="git push"  
    alias gtpullmk="gtpull;./make all"  
    alias gtst="git stash"  
    alias gtstash="gtst save"  
    alias gtstlist="gtst list"  
    alias gtctall="runformatter;git commit -a"
    

  1. Alternatively, you could grep without the anchor as:

    grep gt ~/.bash_profile
    

    Here, you are simply looking for any line that contains the substring "gt", anywhere. As a result, you may get some unnecessary lines, but you will get all aliases that use any of your git aliases.

    On my machine, I get the following output using this search. Notice the extra line at the top, which was not present in the previous output:

    alias debugmk="echows;gtb;${ws}/make ${DEBUG_OPTIONS}"  
    alias gts="git status"  
    alias gtd="git diff"  
    alias gtpull="git pull"  
    alias gtb="git branch"  
    alias gtpush="git push"  
    alias gtpullmk="gtpull;./make all"  
    alias gtst="git stash"  
    alias gtstash="gtst save"  
    alias gtstlist="gtst list"  
    alias gtctall="runformatter;git commit -a"
    

  1. Finally, depending upon which command you find more useful (or even go for both if they both are useful), you can create a new alias to list all git related aliases:

    alias allgit="alias | grep gt"
    
6
  • 2
    Whoa...overly complex! How about just doing alias | grep "^alias gt" instead of processing your bash profile. This will cover all aliases, even the ones not set in .bash_profile
    – Allan
    Apr 6 '20 at 15:55
  • @Allan: Didn't know. Much better. Apr 6 '20 at 15:57
  • 1
    Also you don't need to cat into grep :-)
    – nohillside
    Apr 6 '20 at 17:17
  • @nohillside: Didn't know that either. TY for updating the answer. Apr 6 '20 at 17:20
  • @nohillside: Maybe I should mark this answer Community Wiki now. :D Apr 6 '20 at 17:21
2

You can use the Programmable Completion Builtins compgen command with the a option. The syntax looks like:

compgen [option] [word]

The a option lists all the aliases and word matches all the aliases beginning with those characters. So, to answer your question to list all git aliases:

compgen -a gt

More info can be found with this command:

help compgen
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .