Be more productive with shell aliases
Tags: discuss, productivity, shell
Published: 2019-01-26 17:02:22

The most valuable time as a software developer is usually time spent collaborating and thinking. To get as much of that time as possible, reduce the time you spend typing things with your mortal fingers!

## Git

Git is a ubiquitous version control system, and becomes more important as many developers begin collaborating on the same code base. If your organization uses Agile development practices, you'll also be creating many branches and updating `master` a lot.

I use [`oh-my-zsh`](https://github.com/robbyrussell/oh-my-zsh)'s [Git plugin](https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git.plugin.zsh) which provides an amazing variety of aliases for common (and not-so-common) Git subcommands. A common workflow in git is to:

1. Check out the `master` branch
1. Update your local `master` branch with the latest changes from the remote
1. Create a new branch for your changes
1. Commit your changes with a [good commit message](https://chris.beams.io/posts/git-commit/)
1. Push your branch to the remote

Without any aliases, this workflow looks like this on the command line:

```shell
$ git checkout master
$ git pull origin master
$ git checkout -b my-feature
# Make changes...
$ git add .
$ git commit -am 'Add my feature'
$ git push origin my-feature
```

This is fine, but sometimes I forget that pesky `-b` flag when creating a branch and also get annoyed that I need to state which branch I'm pushing when it's almost always the current one.

With the Git plugin aliases, this workflow takes a lot less work:

```shell
$ gcm
$ gl origin master
$ gcb my-feature
# Make changes...
$ ga .
$ gcam 'Add my feature'
$ ggpush  # this takes advantage of oh-my-zsh's git_current_branch
```

Sometimes you need to stash your changes while looking at another branch or doing more complex things in Git:

```shell
$ git stash
# Do some stuff and come back
$ git stash pop
```

But these aliases come in handy:

```shell
$ gsta
# Do some stuff and come back
$ gstp
```

There are a lot of handy aliases, including plenty with the various flags you might use for a particular Git subcommand:

```shell
$ gd  # git diff
$ gdca  # git diff --cached
$ gds  # git diff --staged
```

I love using these, and the shorthand for them is generally intuitable kind of like Vim. I've added a few of my own for command+flag combinations I use commonly, like:

```shell
$ goop  # "git oops": git reset --soft HEAD~1
$ gun  # "git unstage": git reset HEAD --
```

I also checkout and update master then prune cleaned up remote branches so often that I made a single command, [`upr`](https://github.com/daneah/dotfiles/blob/4b0b691013a8dcf5f3ef6e8f44157187ba6e14d2/source/functions.zsh#L10) (for "update repo"), that does this all for me.


## Python

I work in Python and Django most days. The ease and length of my aliases in this arena reflect that; the more frequently I run a command, the shorter its alias:

```shell
$ python manage.py runserver  # Nope
$ ./manage.py runserver  # Nope
$ run  # There we go
```

```shell
$ pipenv shell  # Nope
$ psh  # Yep
```

```shell
$ pip install -r requirements.txt  # Hmmm
$ prq  # Indeed!
```


## Odds and ends

There are a few general command utilities that I use often enough to slim them down too. The one I use the absolute most is `named`, which expands to `find . -name`. Then I just ask for files `named "*.py"`! I also default `grep` to using colors and line numbers by aliasing it to `grep -n --color=auto`. I can use the bare `grep` by typing `\grep` on the rare occasions where it's needed.

You can see all of my aliases [here](https://github.com/daneah/dotfiles/tree/master/source/aliases.zsh).


## Conclusion

The point of all this is that you should spend time _thinking_ about what you want to do and as little time as possible _doing_ what you want to do when using the command line or writing code. I'm able to reincorporate all the time I save into more thinking&mdash;usually about all the bugs I've introduced 😉