Use the Terminal cheat Tool to Generate CheatSheets for Commands

Mar 14, 2024 - 2 Comments

How to use the cheat command to generate cheatsheets for the command line on Mac

While there are some command line wizards out there who never need to look at references, search manual pages, defer to command lists, LLM queries, or web searches, the rest of us mere mortal commands line users can be helped by a really great tool called cheat. The cheat command is able to provide a cheatsheet with usages, flags, and options for using varying commands, which should be useful for just about anyone who finds themselves in the Terminal. And the examples provided by cheat are what is commonly used, making it much more helpful and intuitive than a man page.

Like all Terminal and command line tasks, this is aimed at more advanced users, though the utility of the cheat command is obviously beneficial to those who are less familiar with the command line or who are simply learning to use the Terminal as well.

Let’s dive in and learn how you can install cheat on your Mac, and use cheat to generate command line cheatsheets for command reference.

How to Install cheat for Terminal Cheatsheets on Mac

The simplest way to install cheat is by using Homebrew. Thus, you will want to install HomeBrew on Sonoma or Ventura or older Macs first if you have not done so already.

Assuming you have HomeBrew installed, installing cheat is simple:
brew install cheat

Once cheat installs, you’ll then want to run cheat so that it can download some community cheatsheets. An easy way to do this is to run the -l flag with cheat:

cheat -l

Hit “y” to agree to installing community cheatsheets.

How to Use cheat to Generate Command Line Cheatsheets in MacOS Terminal

Now you’re ready to use cheat, and it’s pretty easy. Just feed the cheat command another command that you want to have a cheatsheet for. Like so:

cheat (command)

For example, to generate a cheatsheet for du:

cheat du

Or to create a cheatsheet for du:

cheat du

What you will see reported back in Terminal is a simple easy to use cheatsheet with examples of the du command, like so:

$ cheat du
# To sort directories/files by size:
du -sk *| sort -rn

# To show cumulative human-readable size:
du -sh

# To show cumulative human-readable size and dereference symlinks:
du -shL

# Show apparent size instead of disk usage (so sparse files will show greater
# than zero):
du -h --apparent-size

# To sort directories/files by size (human-readable):
du -sh * | sort -rh

# To list the 20 largest files and folders under the current working directory:
du -ma | sort -nr | head -n 20

You can even edit the cheatsheets with your own examples, which is wildly useful for systems administrators or tinkerers who want to have specific instruction for things unique to their environment.

To edit a cheatsheet, use the -e flag followed by a command.

cheat -e command

This is a really powerful tool for command line users that you’re sure to appreciate, whether you’re tinkering with your own system, doing administration with other machines, or confronted with a dire situation where humanity rests on your ability to execute a command without a reference like this humorous XKCD comic:

XKCD comic about saving humanity if you can remember a command without reference

You can also install cheat manually if you prefer to do that, but Homebrew is such a convenient and easy to use package manager with so many great packages and tools that it’s hard to recommend another approach. If you’re interested in checking out the project or compiling it manually, head over to the projects GitHub page here.

Have you ever used the cheat command? Do you have another preferred reference approach when using the command line? Share with us your experiences and thoughts in the comments.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

2 Comments

» Comments RSS Feed

  1. Jan Steinman says:

    I used Homebrew to install cheat.

    However, “cheat -l” does *not* “download community cheat sheets.”

    I get this, instead:

    “failed to list cheatsheets: failed to load cheatsheets: failed to load sheet: ip, path: /Users/jan/.config/cheat/cheatsheets/community/ip, err: failed to parse front-matter: failed to unmarshal frontmatter: yaml: unmarshal errors:
    line 1: cannot unmarshal !!str `tags [ …` into sheet.frontmatter”

    So, how do I go about getting these “community cheat sheets?”

    I note that installing cheat did not install a man page for it…

    • Jan Steinman says:

      I searched the web, and found that there is something wrong with some of the “community” files. There was also problems with the distributed conf.yml file.

      The problem is apparently lines at the head of each cheat file, like this:


      tags: [ compression ]

      This is apparently bad YML syntax. Removing this makes the file work.

      Bottom line, I managed to sort out the problems with the community files and edit conf.yml to the point that I got cheat working. But it would be nice if the downloaded install worked without messing with it!

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site