loading...

Time to cheat in the shell

greyhoundforty profile image Ryan Tiffany ・3 min read

Like most people that work in the cloud there are some commands that just flow from my fingers without even a moments hesitation, while others inevitably lead to the man pages or googling. Today we're going to look at a cool little utility for when you just need to grab the proper flags for a command or need a quick refresher on the proper syntax for your fingers to perform their magic. All hail the cheat command.

cheat allows you to create and view interactive cheatsheets on the command-line. It was designed to help remind nix system administrators of options for commands that they use frequently, but not frequently enough to remember.

Installation

cheat has no dependencies. To install it, download the executable from the releases page and place it on your PATH.

Adding Community Cheat Sheets

By default cheat itself does not come with any cheatsheets however there is a large number of community provided ones hosted on Github.

$ cd ~/.config/cheat/
$ git clone https://github.com/cheat/cheatsheets

Now you will need to edit the default ~/config/cheat/conf.yml file to ensure that the community cheatpath points to ~/.config/cheat/cheatsheets. If everything is set up correctly the community cheatpath entry should look like this:

  - name: community
    path: ~/.config/cheat/cheatsheets
    tags: [ community ]
    readonly: true

With the community added sheets in the correct location you can use the command cheat -l to view them:

$ cheat -l
title:        file:                                               tags:
7z            /Users/ryan/.config/cheat/cheatsheets/7z            community,compression
ab            /Users/ryan/.config/cheat/cheatsheets/ab            community
alias         /Users/ryan/.config/cheat/cheatsheets/alias         community
ansi          /Users/ryan/.config/cheat/cheatsheets/ansi          community
apk           /Users/ryan/.config/cheat/cheatsheets/apk           community,packaging
apparmor      /Users/ryan/.config/cheat/cheatsheets/apparmor      community
apt           /Users/ryan/.config/cheat/cheatsheets/apt           community,packaging
apt-cache     /Users/ryan/.config/cheat/cheatsheets/apt-cache     community,packaging
apt-get       /Users/ryan/.config/cheat/cheatsheets/apt-get       community,packaging
aptitude      /Users/ryan/.config/cheat/cheatsheets/aptitude      community,packaging
aria2c        /Users/ryan/.config/cheat/cheatsheets/aria2c        community
asciiart      /Users/ryan/.config/cheat/cheatsheets/asciiart      community
asterisk      /Users/ryan/.config/cheat/cheatsheets/asterisk      community
....

Diving in

One of the commands that I always struggle with is sed. I can never remember the syntax no matter how many times I've tried. Cheat to the rescue:

$ cheat sed
# To replace all occurrences of "day" with "night" and write to stdout:
sed 's/day/night/g' file.txt

# To replace all occurrences of "day" with "night" within file.txt:
sed -i 's/day/night/g' file.txt

# To replace all occurrences of "day" with "night" on stdin:
echo 'It is daytime' | sed 's/day/night/g'

# To remove leading spaces
sed -i -r 's/^\s+//g' file.txt

# To remove empty lines and print results to stdout:
sed '/^$/d' file.txt

# To replace newlines in multiple lines
sed ':a;N;$!ba;s/\n//g'  file.txt

Creating Custom CheatSheets

Create your own cheatsheets using the -e flag. This will open a new file with your default editor and place the new cheatsheet in the personal cheatsheet path. For instance at my job I often have to deal with authenticating against our IAM offering. No matter how many times I do this the command just don't stick for me so I decided to create a cheatsheet for that reason:

cheat -e ibmcloud-iam

Using the -e flag opens a new file in your default editor. The file is stored in the ~/.cheat directory as a regular text file.

$ cheat ibmcloud-iam
# Get IAM Token from API Key
curl -s -k -X POST --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" --data-urlencode "apikey=${IBMCLOUD_API_KEY}" "https://iam.cloud.ibm.com/identity/token"

Discussion

pic
Editor guide