Cheat - Create and View Cheatsheets on Command Line

cheatsheet command lineEver Linux user can admit often forgetting Linux commands or their options. Often we have to check Google or Askubuntu and some other places to remind us of some commands. Personally, I don't seem to keep "# add-apt-repository" in my head. If you face the same problem most terminal users face, there's good news for you. There's no need trying to refer to external resources for *nix commands anymore. The cheat helps you create and view interactive cheatsheets on the command-line. In this article we'll delve deeper into the cheat command and how to create and modify cheatsheets.

Installing cheat

The cheat command allows you create and view interactive cheatsheets right in the terminal. It prevents users and administrators from getting confused while typing commands - especially for administrators whose commands require extra caution.

Cheat was written in Python, hence to use the cheat command, you have to install its dependencies.

# pip install cheat

To install cheat manually from the source enter these commands:

# pip install docopt pygments appdirs
# git clone https://github.com/chrisallenlane/cheat.git
# cd cheat
# python setup.py install

Cheat Usage with Examples

Viewing Cheatsheet for a Command

Typing cheat with a command name shows you all useful options you can use. In this example, we'll use youtube-dl.

$ cheat youtube-dl

# To download a video in 720p MP4:
 youtube-dl -f 22 example.com/watch?v=id

# To download a video in 720p MP4 or WebM or FLV:
 youtube-dl -f 22/45/120

# To list all available formats of a video:
 youtube-dl -F example.com/watch?v=id

# To download a video to /$uploader/$date/$title.$ext:
 youtube-dl -o '%(uploader)s/%(date)s/%(title)s.%(ext)s' example.com/watch?v=id

# To download a video playlist starting from a certain video:
 youtube-dl --playlist-start 5 example.com/watch?v=id&list=listid

# To simulate a download with youtube-dl:
 youtube-dl -s example.com/watch?v=id

# For all video formats see
# http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs

Viewing Available Cheatsheets

To view all available cheatsheets, use the -l option. This displays the names of the command on the first column and the path of the source file on the last column.

$ cheat -l

Creating and Editing Cheetsheets

You can create you own cheatsheets or edit existing ones using the -e option. Cheatsheets are stored in the ~/.cheat/ directory, and are named according to their respective command names - e.g the tar cheatsheet resides in ~/.cheat/tar. We'll try to create a cheatsheet in this example.

We first need to add our preferred cheatsheet editor before we can create or edit a cheatsheet. Edit ~/.bashrc using any text editor and add the following lines

export CHEAT_EDITOR=/usr/bin/gedit

Note that you can use any text editor. In this example, we choose to use gedit. When we try to create or edit a cheatsheet, the specified cheat editor will open up.

$  cheat -e screenfetch
$  cat > ~/.cheat/screenfetch
This is a sample screenfetch cheatsheet

Now, let's try to view our cheatsheet.

$ cheat screenfetch
 This is a sample screenfetch cheatsheet

We edit cheatsheets using the same -e switch.

Searching for Cheatsheets

Cheat has an awesome search feature for commands which match the string you've provided. The -s option is responsible to handling search. In this example, I'll search for all commands which contain "editor"

$ cheat -s "editor"
 git:
 # To set your editor:
 git config --global core.editor emacs

hub:
 → (opens a text editor for your pull request message)

jrnl:
 # Add entry to default jrnl (from your configured text editor)
 "editor": "F:\\Powerpack\\Sublime\\sublime_text.exe -w"
 "editor": "/usr/bin/sublime -w"

Configuring Cheat

You can modify the various cheat defaults. Edit the ~.\bashrc file and add your custom cheat settings there.

Setting a Default Cheat Directory

All your personal cheatsheets are saved in the ~/.cheat directory by default, but you can specify a different path by specifying another DEFAULT_CHEAT_DIR environment variable:

export DEFAULT_CHEAT_DIR='/path/to/cheat/dir'

Setting a Cheatpath

You can instruct cheat to look for cheatsheets in other directories by exporting a CHEATPATH environment variable:

export CHEATPATH='/path/to/your/cheats'

You can specify multiple directories by exporting the CHEATPATH environment variable

export CHEATPATH="$CHEATPATH:/path/to/more/cheats"

Viewing your Cheat Directories

You can tell which directories cheat is looking at using the -d option

$ cheat -d
 /home/eyram/.cheat
 /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets

Enabling Syntax Highlighting

If you want to apply syntax highlighting to your cheatsheets, export a CHEATCOLORS environment variable:

export CHEATCOLORS=true

By default, the bash highlighter is used, but you can manually specify which syntax highlighter to use for each cheatsheet by wrapping its contents in fenced code blocks.

Cheat Version and Help

-v prints the installed cheat version number

$ cheat --version
 cheat 2.2.1

The -h option helps you with the available options cheat provides.

In this article, we looked at a powerful, yet handy utility called cheat which gives provides us with existing cheatsheets for command commands and allows us to create our own as well. You can use cheat for more than just commands. A recipe can go in there too. I personally find this more convenient than man pages or help. What do you think about cheat?

About Eyram Amedzor

Kwaku Eyram has been a working with computers and software for more than 10 years. He's an all-round geek and very fluid with Linux, Android and Windows operating systems. Asides being a tech enthusiast, Kwaku doubles as a tech support personnel and a tech writer. He currently has interest in IoT, web development and information security.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.