Howardism Musings from my Awakening Dementia
My collected thoughts flamed by hubris
Home PageSend Comment

zsh: The Git Module

Zsh tab completion is great for git. For instance, suppose you typed git commit -- (with two dash characters at the end), and at that point hit the tab key. You will see the available options for the commit action:

$ git commit --❚
--all                  -- stage all modified and deleted paths
--allow-empty          -- allow recording an empty commit
--allow-empty-message  -- allow recording a commit with an empty message
--amend                -- amend the tip of the current branch
--author               -- override the author name used in the commit
. . .

One thing I just learned about the Zshell tab completion is that it doesn't just work from the end. In one of my "journal directory", I label everything, entry_001.org, entry_002.org, etc.††Yeah, I'm not sure what I'll do when I reach 1,000 entries. I guess I just don't think I'll be keeping it up that long If I want to edit entry_157.org, I can just type 157 and hit the tab key. Typing just 15 shows me this list:

$ e 15❚
entry_015.org entry_150.org entry_152.org entry_154.org
entry_156.org entry_158.org entry_115.org entry_151.org
entry_153.org entry_155.org entry_157.org entry_159.org

But the tab completion feature for git does not come from the git module from Oh My Zsh. That module just has around a billion aliases for git commands, for instance:

alias gst='git status'
alias gl='git pull'
alias gup='git pull --rebase'
. . .

These aliases seem like a pretty good idea… except for remembering to use them… and hopefully they won't conflict with any aliases you currently use. As you may have noticed, my .zshrc file already has a g alias, but it uses Huy Nguyen's bashmarks project.

However, if you paid attention to the git commands you used often, and created aliases yourself, you have a higher chance to use them. What if we had the computer do the paying attention?

It already does, in your ~/.zsh_history file, so a quick analysis should give you ideas for what you should alias first:

grep ';git ' ~/.zsh_history | \
    cut -d' ' -f3- | cut -d'-' -f1 | sort | uniq -c

The grep command grabs just the git calls, and the first cut removes the actual git from the beginning of the line. The second cut removes any options (but not file names or URLs‡‡That is left as an exercise to the reader. ). The sort and uniq is just a way to help narrow the focus. The -c option to uniq will tell you the number of occurences… if you type git status 80 times (like I did this last week), an alias may be in order.

Since I've been actually busy programming, I'm just going to leave you with this simple idea this week.

Tell others about this article:
Click here to submit this page to Stumble It