A tool showing cheatsheet for programming languages and Linux commands

cheat.sh is a pretty useful tool that provides a cheat sheet for all you need like Linux commands, programming languages and DBMSes. You can use it to display usages of a Linux command, a code snip of a programming language like code for looping an array with PHP.

It collects cheat sheets from best community-driven sources like Learn X in Y, TLDR, StackOverflow. For the moment, it covers 56 programming languages(C, awk, bash, PHP, Python, Perl, R, Ruby, JS, Go, Swift…) and 1000+ Linux commands. If t is generated on the fly, based on available cheat sheets and answers on StackOverflow.

It provides several ways to use it:

  • A command line client , you can install it in your local machine.
  • A web page https://cheat.sh, it can be accessed with a command line curl cheat.sh/<your query string>, that means you can use it anywhere in a terminal without installation.
  • Plugins for IDEs or editors like Vim, Emacs, Subline, VS Code, IDEA, Qtcreator.

Web page usage

There are four formats of URL to use in a command line to get a cheatsheet from the web page(http://cht.sh): long, short, http or https.

$ curl cheat.sh/find
$ curl cht.sh/find
$ curl https://cheat.sh/fnid
$ curl https://cht.sh/find

If you forget how to use cht.sh:

$ curl cht.sh/:help
Usage:
    
    $ curl cheat.sh/TOPIC       show cheat sheet on the TOPIC      
    $ curl cheat.sh/TOPIC/SUB   show cheat sheet on the SUB topic in TOPIC      
    $ curl cheat.sh/~KEYWORD    search cheat sheets for KEYWORD

Options:

 ...
 

Get cheatsheet for a command

Examples with output:

$ curl cht.sh find
# find
# Search for files in a directory hierarchy

# Find files by case-insensitive extension, such as `.jpg`, `.JPG`, & `.jpG`).
# By default, find(1) uses glob pathname pattern matching. To avoid shell
# interpretation, the glob either must be expanded or the string quoted.
find . -iname '*.jpg'

# Find directories.
find . -type d

# Find files. Specifically files; not directories, links, FIFOs, etc.
find . -type f

...

If you don’t know the concise name you need, you can search using ~<keyword> . If your keyword is too general, the result will be long.

# Query a command with keyword "graph"
$ curl cht.sh/~graph
...

 git-log
# Show a graph of commits in the current branch:
git log --graph

# Show a graph of all commits, tags and branches in the entire repo:
git log --oneline --decorate --all --graph

...

Get cheatsheet for a programming language

To query things about a programming language, append the language and query string behind cht.sh with / to separate different parts and + to concatenate query words.

Examples:

# All available cheat sheet for PHP
$ curl cht.sh/php/:list

# Query Lambda in PHP
$ curl sht.sh/php/lambda

# Query how to loop an array with PHP
$ curl cht.sh/php/traverse+array

# To elimieliminate comments use "?" plus "Q" option
$ curl cht.sh/php/lambda/?Q

# If you do not like the answer, you can pick another one by appending a number.
$ curl cht.sh/python/random+string
$ curl cht.sh/python/random+string/1
$ curl cht.sh/python/random+string/2

# List all available cheatsheets for a programming language
$ curl cht.sh/php/:list

Output:

#======================================
# Query Lambda in PHP
$ curl sht.sh/php/lambda
<?
/*
 * What use is lambda in PHP?
 *
 * Anything that requires a temporary function that you probably will
 * only use once.
 *
 * I would use them for callbacks, for functions such as:
 *
 *     * usort (http:php.net/usort)
 *     * preg_replace_callback (http:php.net/preg_replace_callback)
 *
 * E.g.
 */

usort($myArray, function ($a, $b) {
    return $a < $b;
});

...

#======================================
# Query how to loop an array with PHP
$ curl cht.sh/php/traverse+array
<?
/*
 * Loop through an array php
 *
 * Using foreach loop without key
 */

foreach($array as $item) {
    echo $item['filename'];
    echo $item['filepath'];

    // to know what's in $item
    echo '<pre>'; var_dump($item);
}
...

#======================================
# Query without text comment
$ curl sht.sh/php/lambda
<?
usort($myArray, function ($a, $b) {
    return $a < $b;
});

...

Tips: If there is no cheat sheet for a programming language query, it will get a best answer from its sources including StackOverflow. In some means, maybe it can be a dynamic command line search?

Command line client installation and usage

To install it locally, just download cht.sh file and make it is executable. For Windows users, download cht.exe (in such case, run it with cht not cht.sh), or if you are using Git for Windows, you can install it to Git bash like below.

# Download cht.sh to your usr/bin folder under Git
$ curl https://cht.sh/:cht.sh > /d/Program Files (x86)/Git/usr/bin/cht.sh

For Linux users:

# Install it for current user
$ mkdir -p ~/bin/
$ curl https://cht.sh/:cht.sh > ~/bin/cht.sh
$ chmod +x ~/bin/cht.sh

# Install it for all users
$ curl https://cht.sh/:cht.sh | sudo tee /usr/local/bin/cht.sh
$ chmod +x /usr/local/bin/cht.sh

Then you are ready to use it. The difference is just use a space to separate words.

# Get cheatsheet for a command
$ cht.sh find

# Get cheatsheet for a programming language
$ cht.sh php traverse array

# With options
$ cht.sh --help

It is able to enter a special shell mode (requires package “rlwrap”) with a persistent queries context, so you do not need input the same programming language name repeatedly.

$ cht.sh --shell
    cht.sh> cd php
    cht.sh/php> help   # Check other internal commands
    help    - show this help
	hush    - do not show the 'help' string at start anymore
	...
	
    cht.sh/php> traverse array
    ...

    cht.sh/php> /js    # Switch to another programming language
    cht.sh/js> array
    ...
    
    cht.sh/js> ccopy   # Copy the last answer without comments to clipboard
    cht.sh/js> exit    # Exit shell more

Tips:

If you download cht.sh to local, you’d better add an alias such as cht for cht.sh. Then you run it with fewer keystrokes like $ cht find instead of $ cht.sh find.

Make Git bash on Windows better tells how to add an alias to Git bash on Windows.

More resources

cheat.sh project

  • Stealth mode

In this mode, as soon as you select some text with the mouse (and thus adding it into the selection buffer of X Window System or into the clipboard) it’s used as a query string for cheat.sh, and the correspondent cheat sheet is automatically shown. To enter this mode, in shell mode type stealth Q.

Other cheatsheets

  • devhints.io, an online cheatsheet for

    • CLI
    • apps(atom, vscode, sublime-text, …)
    • languages(html, javascript, css, python, rail, go …)
    • libs/frameworks(node.js, angular.js, gulp, react, bootstrap, flow, jquery, …)
    • database(postgresql-json, mysql, postgresql)
  • navi, an interactive cheatsheet tool for the command-line and application launchers.

There are many similar projects out there (bro, eg, cheat.sh, tldr, cmdmenu, cheat, beavr, how2 and howdoi, to name a few). Most of them provide excellent cheatsheet repositories, but lack a nice UI and argument suggestions.

Git bash on Windows

August 12, 2020 cmd dev git-bash

A universal document converter -- Pandoc

Pandoc is a powerful document conversion tool, it is called swiss-army knife in this field. It understands many document formats, like markdown, ms word, pdf, html, etc. And it is free, you may install it as a command tool or try it online.

cmd dev

Tools to manage code snippets

2 apps to manage your code snippets in local machine: massCode and SnippetStore. Both tools are open source apps with different features. You can choose one according your needs.

dev

A tool to learn Git

LearnGitBranching is an online tool that provides an visual and interactive way to learn Git. It is a pure clientside application written in JavaScript, only an HTML page with some JS and CSS, there's no AJAX requests. Just open it in a browser and you are ready to use it. It offers various levels for you to challenge. See more on its GitHub repository.

dev git