Cheat.sh: Command line and programming cheat sheets

I learnt about this awesome project this week, if you code or use the command line at all you may find it helpful. :slight_smile:


Cheat.sh is a project which aims to provide cheat sheets for command line tools and programming languages.

It can be used on the command line as long as you have a HTTP client like curl or wget installed. E.g.,

$ curl cht.sh/pwd
 cheat:pwd 
# Show the absolute path of your current working directory:
pwd

 tldr:pwd 
# pwd
# Print name of current/working directory.

# Print the current directory:
pwd

# Print the current directory, and resolve all symlinks (i.e. show the "physical" path):
pwd -P

It can also be used from a browser by visiting https://cht.sh, or there is a command line client and integrations for several editors.

It also has sheets for many programming languages. E.g.,

$ curl cht.sh/c/strcmp
/*
 * c - Implementation of strcmp
 * 
 * Uhm.. way too complicate. Go for this one:
 */

int strCmp(const char* s1, const char* s2)
{
    while(*s1 && (*s1 == *s2))
    {
        s1++;
        s2++;
    }
    return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}

/*
 * It returns <0, 0 or >0 as expected
 * 
 * You can't do it without pointers. In C, indexing an array is using
 * pointers.
 * 
 * Maybe you want to avoid using the * operator? :-)
 * 
 * [Gianluca Ghettini] [so/q/34873209] [cc by-sa 3.0]
 */

or ask it questions, for example

$ curl cht.sh/python/parallel-programming
#  question_id: 20548628
#  You can use the [multiprocessing](http://docs.python.org/2/library/mul
#  tiprocessing.html) module. For this case I might use a processing
#  pool:

 from multiprocessing import Pool
 pool = Pool()
 result1 = pool.apply_async(solve1, [A])    # evaluate "solve1(A)" asynchronously
 result2 = pool.apply_async(solve2, [B])    # evaluate "solve2(B)" asynchronously
 answer1 = result1.get(timeout=10)
 answer2 = result2.get(timeout=10)

#  This will spawn processes that can do generic work for you. Since we
#  did not pass `processes`, it will spawn one process for each CPU core
#  on your machine. Each CPU core can execute one process simultaneously.
#  
#  If you want to map a list to a single function you would do this:

 args = [A, B]
 results = pool.map(solve1, args)

#  Don't use threads because the
#  [GIL](https://wiki.python.org/moin/GlobalInterpreterLock) locks any
#  operations on python objects.
#  
#  [Matt Williamson] [so/q/20548628] [cc by-sa 3.0]
$ 

(it searches stack overflow for you!)

or even

$ curl cht.sh/matlab/:learn | more

if you’re into that sort of thing :wink:

1 Like

Excellent find! I can’t quite grasp the meaning of the colon-headed keywords (I suppose it’s a marker of meta topics), but I see
cht.sh/:help
is there for me.

Also, as entry points:
cht.sh/:intro
cht.sh/:list

It’s good to see there are topics on awk and emacs… and other programming languages.

Oh, and stealth mode for online interviews - not so sure about that! (It monitors X’s selection buffer and responds silently in your terminal - no typing needed.)

2 Likes

The code snippets look slightly mind-blowing for getting up and running quickly (clearly not all of them will work out of the box, but still).

The editor integrations on the readme look great for quickly hacking something up (or when you switch languages a lot, start typing in the wrong language & need a nudge).

The VSCode extension (vscode-snippets) uses cht.sh to get them directly into the editor via the command menu. It has a nice “find snippet based on selected text”, which looks like a good tool when you come across some esoteric code in a project.
(it’s just a creative way of presenting cht.sht but seems good)