Tutorial

How To Install and Use Command Line Cheat Sheets on Ubuntu 14.04

Published on July 21, 2015
How To Install and Use Command Line Cheat Sheets on Ubuntu 14.04

Introduction

Cheat is a command line based Python program that allows system administrators to view and store helpful cheat sheets. It retrieves plain-text examples of a chosen command in order to remind the user of options, arguments, or common uses. Cheat is ideal for “commands that you use frequently, but not frequently enough to remember.”

Sheets are small portable text files that can be copied across multiple Linux/Unix systems; they are called and viewed like any other command line program. Base sheets for common programs are provided but you can add custom new sheets, too.

Prerequisites

To follow this tutorial, you will need:

Step 1 — Installing Cheat

Before installing Cheat, we need make sure everything’s up to date on the system.

  1. sudo apt-get update && sudo apt-get upgrade

Confirm by entering y for any prompts in this step.

Installing Cheat is best done with the Python package manager Pip, so install Pip next.

  1. sudo apt-get install python-pip

Cheat itself only depends upon two Python packages, both of which are conveniently included with Pip’s Cheat package. Finally, install Cheat.

  1. sudo pip install cheat

A successful install of Cheat will output these lines:

sudo pip install cheat output
Successfully installed cheat docopt pygments
Cleaning up...

We can confirm that Cheat is installed and working by running it with its -v option.

  1. cheat -v

This outputs the version of Cheat that we have installed.

cheat -v output
cheat 2.1.10

Step 2 — Setting the Text Editor

Before we can go on to create our own cheat sheets, Cheat needs to know which text editor we would like to use to edit sheets by default. To do this, we must create and set an environment variable called EDITOR. For more information on shell and environment variables, you can read the How To Read and Set Environmental and Shell Variables tutorial.

Because nano is already installed on Ubuntu and is generally easy to learn, we’ll set it as our preferred text editor with the command below. However, you can use vim, emacs, or your favorite text editor instead.

  1. export EDITOR="/usr/bin/nano"

We can confirm this was successful by typing:

  1. printenv EDITOR

This will output the new $EDITOR environment variable’s contents:

printenv EDITOR output
/usr/bin/nano

To make this change persistent and permanent across all future shell sessions, you must add the environment variable declaration to your .bashrc file. This is one of several files that are run at the start of a bash shell session.

Open this file for editing:

  1. nano ~/.bashrc

Then add the same export command:

~/.bashrc
. . .
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

export EDITOR="/usr/bin/nano"

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
. . .

Save and exit the file by pressing CTRL+X and then Y followed by ENTER.

Step 3 — Customizing Cheat (Optional)

In this step, we’ll customize Cheat by enabling syntax highlighting and command line auto-completion.

When using a terminal emulator that has color support, you can enable syntax highlighting for your sheets by exporting a shell environment variable named CHEATCOLORS defined as true:

  1. export CHEATCOLORS=true

Now whenever you retrieve cheat sheets, they will be formatted with colored syntax highlighting. If you like this feature, you can make it persistent and permanent across shell sessions by adding the export command to your .bashrc file.

Open the .bashrc file again:

  1. nano ~/.bashrc

Then add the new CHEATCOLORS variable below the EDITOR variable:

~/.bashrc
. . .
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

export EDITOR="/usr/bin/nano"
export CHEATCOLORS=true

# don't put duplicate lines or lines starting with space in the history
. . .

Save and close the file.

Next, to enable command line auto-completion, we need to put a script in the /etc/bash_completion.d/ directory. Change to this directory.

  1. cd /etc/bash_completion.d/

Then download the script we need from Cheat’s GitHub project page.

  1. sudo wget https://raw.githubusercontent.com/chrisallenlane/cheat/master/cheat/autocompletion/cheat.bash

Now enter bash into the current shell to pick up the changes.

  1. bash

Tab auto-completion for Cheat is now enabled. If you type cheat followed by a space, pressing the TAB key twice will give you a list of commands.

cheat tab auto-completion output
cheat 
7z           asciiart     chown        df           du          
grep         indent       jrnl         mkdir        netstat
. . .

Step 4 — Running Cheat

To run Cheat in its most basic form, you call it like any other command, followed by an existing cheat sheet name.

Here is an example of how to do this with one of the default sheets that comes included with Cheat, for the tail command (which outputs the last few lines of a file).

  1. cheat tail

You’ll then see this output:

cheat tail output
# To show the last 10 lines of file
tail file

# To show the last N lines of file
tail -n N file

# To show the last lines of file starting with the Nth
tail -n +N file

# To show the last N bytes of file
tail -c N file

# To show the last 10 lines of file and to wait for file to grow
tail -f file

To see what other other existing cheat sheets are available to us, run Cheat with its -l option.

  1. cheat -l

This lists all available sheets and their location on the server.

Step 5 — Creating and Editing Cheat Sheets

Although the base provisional sheets included with Cheat are useful and varied, they are not all inclusive of every shell command or program available to us. The real benefit we can get from Cheat comes with adding our own custom sheets.

For example, there is no sheet for the networking program ping:

  1. cheat ping
cheat ping output
No cheatsheet found for ping

Let’s make one to serve as an example of how to create and add a new sheet. First, invoke Cheat on the command line again, this time followed by -e and the name of the sheet we are making it for.

  1. cheat -e ping

Cheat will create and open the relevant file for editing using the $EDITOR variable we set earlier.

Add a useful ping command example to the beginning of this new sheet, complete with a comment (indicated by #) that explains what the command does when entered. Here is one such command you could enter in the file:

~/.cheat/ping
# ping a host with a total count of 15 packets overall.    
ping -c 15 www.example.com

Save and exit the file as before. Next let’s test the new sheet by running cheat ping again.

  1. cheat ping

This time, we’ll see the cheat sheet we just added.

cheat ping output
# ping a host with a total count of 15 packets overall.    
ping -c 15 www.example.com

To modify an existing sheet, we can use the -e option again.

  1. cheat -e ping

The ping sheet is now open and we can add more examples or content. For example, we can add the following:

~/.cheat/ping
# ping a host with a total count of 15 packets overall.    
ping -c 15 www.example.com

# ping a host with a total count of 15 packets overall, one every .5 seconds (faster ping). 
ping -c 15 -i .5 www.example.com

Step 6 — Searching Cheat Sheets

Cheat has a built-in search function triggered with the -s option. This will pick up any and all occurrences of the text you provide it with. For example:

  1. cheat -s packets

This command will output all the lines featuring the term “packets” and the sheet they originate from.

cheat -s packets utput
nmap:
  # --min-rate=X => min X packets / sec

ping:
  # ping a host with a total count of 15 packets overall.    
  # ping a host with a total count of 15 packets overall, one every .5 seconds (faster ping). 

route:
  # To add a default  route (which will be used if no other route matches).  All packets using this route will be gatewayed through "mango-gw". The device which will actually be used for that route depends on how we can reach "mango-gw" - the static route to "mango-gw" will have to be set up before.

tcpdump:
  # and other packets being transmitted or received over a network. (cf Wikipedia).

. . .

Conclusion

Because everything Cheat displays is plain-text and directed through the shell’s standard output, we can use any text processing commands (like grep) with it. You can read the Using Grep & Regular Expressions to Search for Text Patterns in Linux tutorial for more information on grep.

Additionally, version control system such as Git with GitHub are ideal for storing your custom cheat sheets centrally, so you can get hold of them on multiple platforms via cloning a repository. A sheet is classed as custom if have you added to it, amended it, or created it yourself through Cheat.

All custom cheat sheets are stored in your Linux user’s home directory, inside a hidden folder named .cheat. You can find this location by running cheat -d, which will output two directories: the first is the location of your custom sheets, and the second is the location of the default sheets you get with Cheat upon install.

To access your library of custom sheets on other systems, you need only to copy this .cheat folder onto them. The cheat sheets are small plain-text files so this makes them perfect for tracking with version control. For a complete solution to making your cheat sheets and configuration files accessible at all times, you can read the How To Use Git to Manage your User Configuration Files on a Linux VPS tutorial.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Scarlz

author


Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Wonder if I can install this Cheat on my Mac by following these procedures? Or need some changes for Mac?

Thanks

another way is to add aliases to $HOME/.bashrc ;) but thats just me :=)

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel