Jake Worth

Jake Worth

Refining Your Terminal Aliases

Published: February 10, 2022 • Updated: June 01, 2023 2 min read

  • terminal

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.

Identify Alias Opportunities With the ‘typeless’ Command

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:

  • “Show the last 20,000 lines of history”
  • “Use the sed stream editor to match just the commands, excluding the history indices”
  • “Sort the commands”
  • “Collapse and count duplicate commands”
  • “Sort by count of duplicates”
  • “Show the last 100 entries”

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.

Solidify the Alias With ‘alias-tips’

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.

Conclusion

To summarize:

  • Find alias candidates with a command like typeless
  • Solidify the alias with Post-Its or ‘alias-tips’

What are your thoughts on aliasing terminal commands? Let me know!


Join 100+ engineers who subscribe for advice, commentary, and technical deep-dives into the world of software.