Published: February 10, 2022 • Updated: June 01, 2023 • 2 min read
Any command you type out manually, or even tab-complete a few times, can be shortened. A common shortening technique is the terminal alias.
Here are some tips that help me write better aliases and cut my terminal keystrokes.
I found this trick a long time ago from, I believe, Thoughbot’s Upcase course.
I call it typeless
, and it’s a well-worn alias in my .zshrc
:
# ~/.zshrc
# Find commands I type often so I can alias them
alias typeless='history n 20000 | sed "s/.* //" | sort | uniq -c | sort -g | tail -n 100'
What’s happening here? In words:
To summarize: “Show me the commands that I type most often into the terminal.” Here are my top ten entries from today:
$ typeless
3 gc
3 gnap
3 history
3 history n 20000
3 man sort
4 gst
10 figlet "DONT PUSH"
10 gp
15 gcheddar
33 gap
I use this information to find commands that are not aliased and aliases them. No
command is too small if it would save time. As you can see, my top three
commands, gap
, gcheddar
, and gp
, are all aliases themselves:
$ gap # git add --patch
$ gcheddar # git commit --amend -CHEAD
$ gp # git push
That’s a good sign; most of the time I’m typing a fraction of the characters that someone without these aliases would type. Here’s how I created one of these aliases:
# ~/.zshrc
alias gap='git add --patch'
Write down your the alias on a Post-It and commit to using it every time until muscle memory takes over. Doing this periodically is one of the best investments I’ve made in my programming speed.
Looking for something smarter than a Post-It? Redditor u/number5 shared a great solution for leveraging the aliases you have. The library alias-tips analyzes your existing aliases and alerts you when you miss a chance to use one.
$ git status
Alias tip: gst
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Here, I entered git status
but I’ve already aliased that to gst
. Using
my existing alias is faster and leverages work I’ve already done.
To summarize:
typeless
What are your thoughts on aliasing terminal commands? Let me know!
Get better at programming by learning with me! Join my 100+ subscribers receiving weekly ideas, creations, and curated resources from across the world of programming.