My ZSH Snippets cover image

My ZSH Snippets

Chris Di Carlo • June 30, 2021

After typing the same commands at a shell prompt or manually configuring some JSON config file for the umpteenth time, I sometimes find myself falling down the rabbit-hole and wanting to make my life easier. Here's 3 snippets I use on a semi-regular basis. Enjoy!

Git Add, Commit, $*#%

I use zsh with Oh-My-Zsh and the git plugin, which has many nice aliases for commands but there are a couple of little quality of life things that kept niggling at me and over time just came to a head.

The git oh-my-zsh plugin provides an alias gcmsg that can be used like so:

gcmsg "Something was changed!"

This is just shorthand for:

git commit -m "Something was changed!"

This works fine but after a while a couple of things kept irritating me:

So once I finally hit that pain threshold, I decided to do something about it, and voila! Let me introduce you to acm:

acm() {
    message=$*
    if [ -z "$message" ]; then
        read -E -r "message?Commit message: "
    fi

    git add --all && git commit -m "$message"
}

What does it do? Nothing fancy, the name kind of says it all: acm == Add-Commit-Message. It just adds all the files to the index and takes whatever you pass into it and wraps it in quotes for the commit message. But the neat part is that if I forget to provide a commit message, it doesn't just fail out - it prompts me to add it.

I don't use it all the time but as long as I make my commits nice and granular and make sure I stay within the feature I'm working on, it's so damn quick.

Speeding Up Package Development

Developing Laravel packages is a pain when you get around to testing them within the context of a real app. You need to add a VCS repository (unless it's published to Packagist or elsewhere), require the package, etc. But then every time I find a bug that needs to be fixed, I need to do a composer update. The feedback loop takes a real hit in productivity. Then I stumbled upon a small snippet that makes life so nice I can't believe I didn't look for a solution earlier. I confess this is not original: I saw it in a post from Caleb Porzio (Here's the original blog post if you're interested). When I saw how old the post was, I kicked myself. Hard. Twice.

The only change I made was making the name of the repo a parameter; otherwise, I couldn't link multiple paths due to the hard-coded name. Here's my modified snippet:

composer-link() {
    composer config repositories.$1 '{"type": "path", "url": "'$2'"}' --file composer.json
}

And here's how I use it:

composer-link package ~/package

Private Packages

I now use repman.io for my private packages; before I had to keep adding a VCS repository for each private package I was using and that got old super fast. After I found the previous snippet, I realized I could do something similar to make it painless when I'm bootstrapping a new app:

composer-repo() {
    composer config repositories.repman '{"type": "composer", "url": "https://my-name.repo.repman.io"}' --file composer.json
}

This works the same as composer-link but there are no parameters because I only ever have the one private package repository. Now I can require my private packages the same way I require public packages. Neat!

Everything In One File

All of these snippets exist in my .zshrc file but they should work in .bashrc or .bash_profile, too.

If you have snippets that you find useful, I'd love to hear! I'm at @chris_di_carlo.

Happy coding! Cheers!