newline

Table of Contents

  1. What is it?
  2. How do you use cheat.sh?
    1. Without curl
  3. Making it more comfortable
    1. In the shell
    2. In Vim
  4. Conclusion

Cheat sheets accessible from anywhere, with cheat.sh

Guide, Shell, Vim

June 24, 2020

Cheat sheets are great. They let you find the most important information about a command at one glance, without having to go scuba-diving in the man page. There are lots of projects that aim to be a package of such cheat sheets: tldr, bro, eg, and cheat, just to name a few. But the downside of all of these is that you have to install something. Most of them have dependencies. You can’t just get a cheat sheet from anywhere, any time. That’s why I use cheat.sh exclusively, which provides cheat sheets in plaintext, accessible with a single HTTP GET request. You can send that request with curl (most common), but you can also use a browser, or any other HTTP tool. Doesn’t matter whether you’re on Ubuntu, Arch, macOS, Windows, Android, Raspberry Pi, or a potato (as long as it has internet access and supports HTTP).

What is it?

cheat.sh aims to be “the only cheat sheet you need”, and in my case, it definitely has been. You don’t need a client, you don’t need dependencies — all you need is a way to make an HTTP GET request. It collects cheat sheets from the aforementioned tldr and cheat pages, as well as other sources.

How do you use cheat.sh?

Well, let’s say I’m trying to disarm a bomb and have to use tar. All I need to do is run the command curl cheat.sh/tar, and I’ll get back text output, listing the key commands and options for tar. So, the basic usage is:

curl cheat.sh/keyword

But cheat.sh is more powerful. If you want to write a script for Neovim but don’t know Lua, run curl cheat.sh/lua/:learn and you’ll get the most important parts of Lua in abridged, plaintext form with syntax highlighting. Or you can search a page: if you want to know how to parse JSON in Python, run curl cheat.sh/python/parse+json. To go to different pages matching your query, append /1, /2, etc. to the URL (like curl cheat.sh/python/parse+json/1, curl cheat.sh/python/parse+json/2).

You can search pages for a keyword, by running curl cheat.sh/~keyword. By default, ~ only searches cheat sheets on the current level, so curl cheat.sh/scala/~variable only searches Scala pages for “variable”. To make the search recursive, add the r modifier at the end of the URL, like curl cheat.sh/~variable/r. Other modifiers are i for case insensitive and b to search at word boundaries, and options can be combined.

And there are many other options, you can look at the README or run curl cheat.sh to learn more.

Without curl

Don’t have curl and can’t install it? No issue. You can send an HTTP request with telnet or netcat, as long as you send the right headers. For example, with netcat, run

( printf "GET / HTTP/1.1\r\nHost: cheat.sh\r\nUser-Agent: curl\r\n\r\n"; sleep 1 ) | nc cheat.sh 80

With telnet, it’s similar. Maybe you can even do it manually without a program.

And if you want offline access, just save the output of curl, or whatever other program you’re using, to a file. It’s that simple.

If you want to edit a cheat sheet or create a new one, it’s also easy, and described concisely in this section of the README.

Making it more comfortable

In the shell

Now, cheat.sh does offer a client, but I don’t use or need it. I prefer the simpler alternative — a shell function:

cheat() { curl -Ls cheat.sh/"$1" | ${PAGER:-less -R}; }

This will take whatever you pass it as the first argument, search it on cheat.sh, and pipe the output into your pager, defaulting to less. The -s means to be silent (i.e. don’t show progress), and -L means to follow redirects. For less, the -R option will let you see colors (ANSI).

In Vim

Pretty often, I want to see a cheat sheet without going to a shell, so I made a Vim command to do just that.

command! -nargs=1 Cheat terminal curl cheat.sh/<args>

That way, you can run :Cheat tar in Vim to get the cheat sheet for tar (as long as your Vim supports the :terminal command).

Conclusion

cheat.sh is by far the simplest way to access cheat sheets. It’s fast, plaintext, and doesn’t require any installation. They have a huge amount of cheat sheets from various sources, as well as guides and tips for programming languages from StackOverflow. If you want a cheat sheet repository, this is the one to choose.