Recipes

Copy-pasteable patterns. Each recipe is a complete cheat - drop it into any .md file under your cheats path and it works.


Editable default

Show a default value the user can confirm or overwrite.

## curl: GET with timeout

```sh title:"GET a URL with a timeout"
curl --max-time $timeout $url
```
<!-- cheat
var url     --- --header "URL"
var timeout = echo "10" --- --header "Timeout (seconds)"
-->

echo "default" produces a single line, which CheatMD pre-fills and lets you edit before pressing Enter.


Pick from a list, return a different column

User sees a friendly description; the command gets the short key.

## SSH: connect with auth method

```sh title:"SSH with chosen auth method"
ssh $ssh_flags $user@$host
```
<!-- cheat
var host --- --header "Hostname"
var user = echo "$USER" --- --header "Username"
var auth_method = printf 'key\tUse SSH key (default)\npassword\tUse password\n'     --- --delimiter '\t' --column 2 --select-column 1 --header "Auth method"

if $auth_method == key
  var ssh_flags := -o PreferredAuthentications=publickey
fi

if $auth_method == password
  var ssh_flags := -o PreferredAuthentications=password
fi
-->

See [Selector Options](/docs/selector options) for details on --column vs --select-column.


Pick a Docker container

## Docker: exec

```sh title:"Open a shell in a container"
docker exec -it $container /bin/sh
```
<!-- cheat
var container = docker ps --format "{{.Names}}" --- --header "Container"
-->

If no containers are running, the picker shows nothing and CheatMD falls back to a manual prompt.


Pick a Kubernetes context + namespace

## Kubernetes: get pods

```sh title:"List pods in a namespace"
kubectl get pods -n $namespace --context $context
```
<!-- cheat
var context   = kubectl config get-contexts -o name --- --header "Context"
var namespace = kubectl --context $context get ns -o name     --- --map "cut -d/ -f2" --header "Namespace"
-->

$context resolves before $namespace’s shell runs, so the second var can reference the first. --map "cut -d/ -f2" strips the namespace/ prefix.


Reusable module

Define once in modules.md:

<!-- cheat
export docker_container
var container = docker ps --format "{{.Names}}" --- --header "Container"
-->

Use anywhere:

## Docker: tail logs

```sh title:"Follow logs"
docker logs -f $container
```
<!-- cheat
import docker_container
-->

## Docker: stop

```sh title:"Stop container"
docker stop $container
```
<!-- cheat
import docker_container
-->

See Modules for details.


Path completion for file arguments

## tar: extract

```sh title:"Extract an archive"
tar -xvf $file -C $dest
```
<!-- cheat
var file = find . -maxdepth 1 ( -name "*.tar*" -o -name "*.tgz" ) 2>/dev/null     --- --header "Archive"
var dest = echo "." --- --header "Destination"
-->

While resolving $file or $dest, type a path and press Tab:

./ar<Tab>        -> ./archive.tar.gz
/tm<Tab>         -> /tmp/
$HOME/Down<Tab>  -> $HOME/Downloads/

See [Path Completion](/docs/path completion) for details.


Branching: dev vs prod URL

## API: ping

```sh title:"Ping a service"
curl -s $url/health
```
<!-- cheat
var env = printf 'dev\nstaging\nprod\n' --- --header "Environment"

if $env == dev
  var url := https://api.dev.example.com
fi

if $env == staging
  var url := https://api.staging.example.com
fi

if $env == prod
  var url := https://api.example.com
fi
-->

See Conditionals for details.


Transform with --map

When a column extract isn’t enough.

## AWS: pick bucket

```sh title:"List bucket contents"
aws s3 ls s3://$bucket
```
<!-- cheat
var bucket = aws s3 ls --- --map "awk '{print $3}'" --header "Bucket"
-->

aws s3 ls outputs 2024-01-15 12:00:00 my-bucket per line. --map extracts just the name. See [Selector Options](/docs/selector options) for details.


Chain: multi-step workflow

## Release: choose version

```sh title:"Show release version"
echo $version
```
<!-- cheat
chain release 1
var version --- --header "Version"
-->

## Release: build

```sh title:"Build release artifact"
make build VERSION=$version
```
<!-- cheat
chain release 2
var version --- --header "Version"
-->

## Release: publish

```sh title:"Publish release artifact"
make publish VERSION=$version
```
<!-- cheat
chain release 3
var version --- --header "Version"
-->

Search /chain release in the picker. See Chains for details.


Dump metadata for tools

cheatmd dump ~/cheats --json   # Full metadata as JSON
cheatmd dump ~/cheats --csv    # Flat CSV for spreadsheets/grep

See Dump for field details.