Skip to main content

An advanced snippet manager for the command-line.

Project description

snippet

Parameterized command templates for your terminal.

Save commands once, reuse them forever — with placeholders, defaults, fuzzy search, and composable output.

Usage

$ snippet -e archive/extract-tgz -f 'tar -xzvf <archive>'
$ snippet -t archive/extract-tgz /path/to/foo.tar.gz
tar -xzvf /path/to/foo.tar.gz

Snippet-Cli Preview

Why

Shell aliases are static. History search is fragile. Neither lets you say "run this shape of command, but with these arguments this time."

snippet sits in between: it stores command templates with named placeholders, and fills them on demand. Think of it as printf for your shell history.

Install

pip3 install snippet-cli

Optional — enable tab completion for template names:

eval "$(register-python-argcomplete snippet)"

Quick start

Save a command you'll reuse

# Save or edit a snippet (opens $EDITOR)
$ snippet -e archive/create-tgz -f "tar -czvf <archive> <file...>"

Recall it later

# Exact match
$ snippet -t archive/create-tgz backup.tar.gz file=src/ README.md
tar -czvf backup.tar.gz src/ README.md

# Fuzzy search (opens fzf when no exact match)
$ snippet -t archive

Run it directly

$ snippet -t archive/create-tgz backup.tar.gz file=src/ README.md | bash

Key Features

Permutations and Repeatables

When you pass multiple values to regular placeholders, snippet generates every combination:

$ snippet -f "kubectl cp <file> <namespace>/<pod>:/tmp/<file>" namespace=default pod=example-pod file=busybox nmap tcpdump
kubectl cp busybox default/example-pod:/tmp/busybox
kubectl cp nmap default/example-pod:/tmp/nmap
kubectl cp tcpdump default/example-pod:/tmp/tcpdump

When you add three dots at the end of a placeholder (e.g., <file...>), values are concatenated instead of permuted:

$ snippet -f "tar -czvf <archive> <file...>" backup.tar.gz file=src/ README.md
tar -czvf backup.tar.gz src/ README.md

By default arguments which are associated with a repeatable placeholder are separated by space. To specify a custom character sequence you may use the join codec:

$ snippet -f "nmap -sS -p<port...|join:','> <rhost...>" port=80 443 rhost=192.168.0.1 192.168.0.2
nmap -sS -p80,443 192.168.0.1 192.168.0.2

Default and Optionals

# Default value — used when nothing is provided
$ snippet -f "python3 -m http.server <lport='8000'>"
python3 -m http.server 8000

# Optional section — disappears entirely if placeholder is empty
$ snippet -f "python3 -m http.server[ --bind <lhost>][ <lport>]" lport=4444
python3 -m http.server 4444

If you need square-brackets to be part of the string format you need to escape them accordingly:

$ snippet -f "\[<arg>\]" hello
[hello]

Codecs

Transform placeholder values with piped codecs. Useful when your archive paths contain spaces:

$ snippet -f "tar -czvf <archive|squote> <file...|squote>" backup.tar.gz file="my docs/" notes.md
tar -czvf 'backup.tar.gz' 'my docs/' 'notes.md'

Codecs can be parameterized and are chainable:

$ snippet -f "cp <file|squote> <file|add:'.bak'|squote>" "/path/to/my file"
cp '/path/to/my file' '/path/to/my file.bak'

Run snippet --list-codecs to see all available transforms.

Presets

Built-in dynamic placeholders like <datetime> — handy for timestamped archives:

$ snippet -f "tar -czvf '<datetime>.tar.gz' <file...>" file=src/
tar -czvf '20250302143012.tar.gz' src/

Customize presets in ~/.snippet/snippet_profile.py.

Input from files, env vars, or pipes

# From a file
$ snippet -f "tar -czvf <archive> <file...>" backup.tar.gz file:filelist.txt

# From environment variables
$ export archive=backup.tar.gz
$ snippet -f "tar -czvf <archive> <file...>" file=src/ README.md

# From a pipe
$ echo "tar -czvf <archive> <file...>" | snippet backup.tar.gz file=src/

Organizing templates

Templates live in ~/.snippet/templates/ and support directory-style namespacing:

$ snippet -e archive/create-tgz -f "tar -czvf <archive> <file...>"
$ snippet -e archive/extract-tgz -f "tar -xzvf <archive>"
$ snippet -e net/scan -f "nmap -sS -p <port> <host>"

$ snippet --list-templates
archive/create-tgz
archive/extract-tgz
net/scan

Organizing codecs

Custom codecs can be added into ~/.snippet/codecs/. Here's a minimal example:

import os.path

from snippet.codecs import StringCodec


class Codec(StringCodec):
    """ Extracts the filename from a file path. """

    def __init__(self):
        super().__init__(author="bytebutcher", dependencies=[])

    def run(self, input):
        return os.path.basename(input.encode('utf-8', errors="surrogateescape")).decode('utf-8', errors="surrogateescape")

Advanced usage

snippet supports multi-line format strings:

$ cat input.txt
{
   "foo": "<foo>",
   "bar": "<bar>"
}
$ cat input.txt | snippet egg spam
{
   "foo": "egg",
   "bar": "spam"
}

snippet can be used to easily execute alternating commands in sequence:

$ snippet -f "echo 'hello <arg1>';" arg1=snippet world | bash
hello world
hello snippet

Using xargs the resulting commands can also be executed in parallel:

snippet -f "echo 'hello <arg1>';" arg1=snippet arg1=world | xargs --max-procs=4 -I CMD bash -c CMD
hello world
hello snippet

How it compares

Tool Purpose
shell aliases Static command shortcuts; no parameters
tldr / navi Look up cheat sheets
pet / cheat Save and search snippets; less focus on parameterization
snippet Save, parameterize, compose, and execute your own command templates

See also

To make the most out of this tool you might also consider to look into the following projects:

  • snex - previews and optionally executes snippets
  • mkenv - creates and updates shell environment files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snippet_cli-2.4.0.tar.gz (36.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snippet_cli-2.4.0-py3-none-any.whl (45.4 kB view details)

Uploaded Python 3

File details

Details for the file snippet_cli-2.4.0.tar.gz.

File metadata

  • Download URL: snippet_cli-2.4.0.tar.gz
  • Upload date:
  • Size: 36.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for snippet_cli-2.4.0.tar.gz
Algorithm Hash digest
SHA256 1cd0b14e75ec1384f576ca3c1e6b6a93da52c3bff82c70923d36299d80c8df47
MD5 9494f4773042f40dec9bb8a528f3e49a
BLAKE2b-256 ccc755ba5b43c8b1c9efd3f24101db7f2284402b20035624e30fb43488557583

See more details on using hashes here.

File details

Details for the file snippet_cli-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: snippet_cli-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 45.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for snippet_cli-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97c227da25a66a6b029ca98cf58e062e9899b4aad3a61d8ec41fa13be552fa54
MD5 7055179b030aaf9c81a232b7136aeb7c
BLAKE2b-256 084168914021b4f2a8d78fb3f47c06aebd4851f673ed3d364d7ece5e76feeda0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page